我是编程新手,也是c编程的新手。我试图读取二进制文件然后按位进程。我的代码示例以及我到目前为止编写程序的方式。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <inttypes.h> /* Use C99 format specifiers: "%" PRIu8 "\n", "%" PRIu32 "\n" */
typedef struct{
uint32_t size; /* Unsigned int 4 bytes (32 bits) */
}mp3_Header;
int main (int argc, char *argv[]) {
mp3_Header first;
unsigned int memory_int[4];
FILE *file = fopen( "song.mp3" , "rb" );
/* 1 Byte flags + revision_number 1 Byte + major_version 1 Byte + header_id 3 Bytes = 6 */
if ( fseek( file , 6 , SEEK_SET ) == -1 ) {
fprintf( stderr, "Not able to fseek at possition 6" );
return EXIT_FAILURE;
}
if ( fread( memory_int , sizeof(memory_int) , 1 , file ) != 1) {
printf("Could not read first.size\n");
exit (0);
}
printf ("This is first.size before sync_safe: %u\n", memory_int);
first.size = (memory_int[3] & 0xFF) |
((memory_int[2] & 0xFF) << 7 ) |
((memory_int[1] & 0xFF) << 14 ) |
((memory_int[0] & 0xFF) << 21 );
printf ("This is first.size after sync_safe: %" PRIu32"\n", first.size);
fclose(file);
return 0;
}
当我编译代码时,我收到错误消息:
error: subscripted value is neither array nor pointer nor vector
first.size = (first.size[3] & 0xFF) |
我试图将另一个值声明为:
unsigned int memory_int[4];
当我编译代码时,我收到警告:
warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned int *’ [-Wformat=]
printf ("This is memset memory_int: %u\n", memory_int);
虽然我将%“PRIu32”更改为%u但我仍然收到此警告。
到目前为止,我知道当我使用typedef struct
时,我可以使用name.parameter调用内容,当我在需要时调用first.size
值时程序似乎正常运行。但是当我尝试按位时遇到问题,因为我要求将值分解为位。理论上它应该是可能的,因为我已经将它定义为32位,但实际上我遗漏的东西非常小。我尝试将另一个值分配为uint32_t,并将无条件的int size [4]分配为两次都不成功。有人能解释一下原因以及如何解决这个问题吗?
答案 0 :(得分:1)
您的代码中有一些要修改的内容。
首先,在要读取的文件名称中使用" "
,因为fopen
synax
FILE *fopen(const char *path, const char *mode);
这样,filename
[path
]将为const char *
第二次,在[现有行之后]的代码中添加以下行。
FILE *file = fopen("song.mp3" , "rb" );
if (!file)
{
printf("Unable to open the file for reading\n");
exit (0);
}
它可以帮助您处理一些意外情况,例如“文件不存在”
第三,将memory_int
声明为包含4个元素的unsigned int
数组。
unsigned int memory_int[4] = {0};
最后,代码看起来像这样。
#include <stdint.h>
#include <unistd.h>
#include <inttypes.h> /* Use C99 format specifiers: "%" PRIu8 "\n", "%" PRIu32 "\n" */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <inttypes.h> /* Use C99 format specifiers: "%" PRIu8 "\n", "%" PRIu32 "\n" */
typedef struct{
uint32_t size; /* Unsigned int 4 bytes (32 bits) */
}mp3_Header;
int main (int argc, char *argv[]) {
unsigned int memory_int[4] = {0};
mp3_Header first;
FILE *file = fopen("song.mp3" , "rb" );
if (!file)
{
printf("Unable to open the file for reading\n");
exit (0);
}
/* 1 Byte flags + revision_number 1 Byte + major_version 1 Byte + header_id 3 Bytes = 6 */
if ( fseek( file , 6 , SEEK_SET ) == -1 ) {
fprintf( stderr, "Not able to fseek at possition 6" );
return EXIT_FAILURE;
}
if ( fread( memory_int , sizeof(memory_int) , 1 , file ) != 1) {
printf("Could not read first.size\n");
exit (0);
}
printf ("This is first.size before sync_safe: %u %u %u %u\n", memory_int[0],memory_int[1],memory_int[2],memory_int[3]);
first.size = (memory_int[3] & 0xFF) |
((memory_int[2] & 0xFF) << 7 ) |
((memory_int[1] & 0xFF) << 14 ) |
((memory_int[0] & 0xFF) << 21 );
printf ("This is first.size after sync_safe: %u\n", first.size);
fclose(file);
return 0;
}
希望这有效!!
答案 1 :(得分:0)
排成一行printf ("This is first.size before sync_safe: %u\n", memory_int);
:%u
对于打印memory_int[0]
非常有用。
打印memory_int
的四个元素的一种方法是使用%u %u %u %u
。
我也在按位操作中改变了一些东西。我更喜欢%x
(十六进制)说明符来显示输出:校正按位运算的输出会更容易。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <inttypes.h> /* Use C99 format specifiers: "%" PRIu8 "\n", "%" PRIu32 "\n" */
typedef struct{
uint32_t size; /* Unsigned int 4 bytes (32 bits) */
}mp3_Header;
int main (int argc, char *argv[]) {
mp3_Header first;
unsigned int memory_int[4];
FILE *file = fopen( "song.dat" , "rb" );
/* 1 Byte flags + revision_number 1 Byte + major_version 1 Byte + header_id 3 Bytes = 6 */
if ( fseek( file , 6 , SEEK_SET ) == -1 ) {
fprintf( stderr, "Not able to fseek at possition 6" );
return EXIT_FAILURE;
}
//printf("%u\n",sizeof(memory_int));
if ( fread( memory_int , sizeof(memory_int) , 1 , file ) != 1) {
printf("Could not read first.size\n");
exit (0);
}
printf ("This is first.size before sync_safe: %x %x %x %x\n", memory_int[0],memory_int[1],memory_int[2],memory_int[3]);
memory_int[0]=0x4212;
memory_int[1]=0x4213;
memory_int[2]=0x4214;
memory_int[3]=0x4215;
first.size = (memory_int[3] & 0x000F) |
((memory_int[2] & 0x000F) << 8 ) |
((memory_int[1] & 0x000F) << 16 ) |
((memory_int[0] & 0x000F) << 24 );
printf ("This is first.size after sync_safe: %x\n", first.size);
fclose(file);
return 0;
}
再见,
弗朗西斯
答案 2 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <inttypes.h> /* Use C99 format specifiers: "%" PRIu8 "\n", "%" PRIu32 "\n" */
typedef struct{
uint32_t size; /* Unsigned int 4 bytes (32 bits) */
}mp3_Header;
int main (int argc, char *argv[]) {
mp3_Header first;
unsigned int memory_int[4];
uint32_t memory_int_2[4];
char memory[4];
unsigned char memory_2[4];
FILE *file = fopen( "song.mp3" , "rb" );
/* 1 Byte flags + revision_number 1 Byte + major_version 1 Byte + header_id 3 Bytes = 6 */
if ( fseek( file , 6 , SEEK_SET ) == -1 ) {
fprintf( stderr, "Not able to fseek at possition 6" );
return EXIT_FAILURE;
}
if ( fread( memory_int , sizeof(memory_int) , 1 , file ) != 1) {
printf("Could not read first.size\n");
exit (0);
}
/* 1 Byte flags + revision_number 1 Byte + major_version 1 Byte + header_id 3 Bytes = 6 */
if ( fseek( file , 6 , SEEK_SET ) == -1 ) {
fprintf( stderr, "Not able to fseek at possition 6" );
return EXIT_FAILURE;
}
if ( fread( &first.size , sizeof(first.size) , 1 , file ) != 1) {
printf("Could not read first.size\n");
exit (0);
}
/* 1 Byte flags + revision_number 1 Byte + major_version 1 Byte + header_id 3 Bytes = 6 */
if ( fseek( file , 6 , SEEK_SET ) == -1 ) {
fprintf( stderr, "Not able to fseek at possition 6" );
return EXIT_FAILURE;
}
if ( fread( memory_int_2 , sizeof(memory_int_2) , 1 , file ) != 1) {
printf("Could not read first.size\n");
exit (0);
}
/* 1 Byte flags + revision_number 1 Byte + major_version 1 Byte + header_id 3 Bytes = 6 */
if ( fseek( file , 6 , SEEK_SET ) == -1 ) {
fprintf( stderr, "Not able to fseek at possition 6" );
return EXIT_FAILURE;
}
if ( fread( memory , sizeof(memory) , 1 , file ) != 1) {
printf("Could not read first.size\n");
exit (0);
}
/* 1 Byte flags + revision_number 1 Byte + major_version 1 Byte + header_id 3 Bytes = 6 */
if ( fseek( file , 6 , SEEK_SET ) == -1 ) {
fprintf( stderr, "Not able to fseek at possition 6" );
return EXIT_FAILURE;
}
if ( fread( memory_2 , sizeof(memory_2) , 1 , file ) != 1) {
printf("Could not read first.size\n");
exit (0);
}
printf ("This is memory_int[0] before sync_safe: %u %u %u %u\n", memory_int[0],memory_int[1],memory_int[2],memory_int[3]);
printf ("This is first.size before sync_safe: %" PRIu32"\n", first.size);
printf ("This is memory_int_2[0] before sync_safe with PRIu32: %" PRIu32" %" PRIu32" %" PRIu32" %" PRIu32"\n", memory_int_2[0],memory_int_2[1],memory_int_2[2],memory_int_2[3]);
printf ("This is memory[0] before sync_safe: %d %d %d %d\n", memory[0],memory[1],memory[2],memory[3]);
printf ("This is memory before sync_safe: %s\n", memory);
printf ("This is memory_2[0] before sync_safe: %d %d %d %d\n", memory_2[0],memory_2[1],memory_2[2],memory_2[3]);
printf ("This is memory_2 before sync_safe: %s\n", memory_2);
first.size = (memory_int[3] & 0xFF) |
((memory_int[2] & 0xFF) << 7 ) |
((memory_int[1] & 0xFF) << 14 ) |
((memory_int[0] & 0xFF) << 21 );
printf ("This is first.size after sync_safe: %" PRIu32"\n", first.size);
first.size = (first.size & 0xFF);
printf ("This is first.size after sync_safe: %" PRIu32"\n", first.size);
first.size = (memory[3] & 0xFF) |
((memory[2] & 0xFF) << 7 ) |
((memory[1] & 0xFF) << 14 ) |
((memory[0] & 0xFF) << 21 );
printf ("This is first.size after sync_safe: %" PRIu32"\n", first.size);
first.size = (memory_2[3] & 0xFF) |
((memory_2[2] & 0xFF) << 7 ) |
((memory_2[1] & 0xFF) << 14 ) |
((memory_2[0] & 0xFF) << 21 );
printf ("This is first.size after sync_safe: %" PRIu32"\n", first.size);
fclose(file);
return 0;
}
当我编译代码时,我收到错误消息:
错误:下标值既不是数组也不是指针也不是向量first.size =(first.size [3]&amp; 0xFF)|
我收到此错误的原因是我试图编译像数组一样的字符串。我将其定义为uint32_t size
,我将其称为first.size[3]
。当然它永远不会工作,因为一个是简单的字符串,第二个是数组。
这是我创建的测试代码,作为测试和学习最多的示例,以解决我的问题。由于我刚开始学习编程,我有很多东西需要阅读才能理解和学习。
我希望这个代码示例能够为我帮助其他人。