我正在尝试从 .dat 文件中读取结构...要读取的第一个整数是读数的数量,然后是以下结构格式的数据
struct Oven
{
int rec_no;
int temp;
int status;
char string[10];
};
下面是我读取文件的方法
int read_and_process_file()
{
int counter;
FILE *ptr_myfile;
struct Oven my_record;
ptr_myfile=fopen("data.dat","rt");
//for no of readings
fread(&no_of_readings,sizeof(no_of_readings),1,ptr_myfile);
for ( counter=1; counter <= 50; counter++)
{
fread(&my_record,sizeof(struct Oven),1,ptr_myfile);
name=my_record.string;
printf("%d : ",my_record.rec_no);
fprintf(ptr_myfile2,"%d : ",my_record.rec_no);
printf("%d : ",my_record.temp);
printf("%d : ",my_record.status);
printf("%s\n",name);
}
fclose(ptr_myfile);
return 0;
}
但我在 25 条记录后无法读取超过 25 条记录,它不断重复第 25 条记录...
试过 fseek 但它跳过了一些记录......有什么帮助吗?
这是输出图像
更新(Wildplasser)这是数据的前 0x200 字节(看起来有点不规则......)
00000000 b8 0b 00 00 00 00 00 00 54 00 00 00 01 00 00 00 |........T.......|
00000010 42 6f 62 00 cd cd cd cd cd cd cd cd 01 00 00 00 |Bob.............|
00000020 4d 00 00 00 00 00 00 00 43 4c 4f 53 45 44 00 cd |M.......CLOSED..|
00000030 cd cd cd cd 02 00 00 00 2e 00 00 00 00 00 00 00 |................|
00000040 43 4c 4f 53 45 44 00 cd cd cd cd cd 03 00 00 00 |CLOSED..........|
00000050 5f 00 00 00 01 00 00 00 48 61 72 72 79 00 cd cd |_.......Harry...|
00000060 cd cd cd cd 04 00 00 00 44 00 00 00 01 00 00 00 |........D.......|
00000070 48 61 72 72 79 00 cd cd cd cd cd cd 05 00 00 00 |Harry...........|
00000080 63 00 00 00 01 00 00 00 4a 69 6c 6c 00 cd cd cd |c.......Jill....|
00000090 cd cd cd cd 06 00 00 00 53 00 00 00 01 00 00 00 |........S.......|
000000a0 4a 61 6d 65 73 00 cd cd cd cd cd cd 07 00 00 00 |James...........|
000000b0 56 00 00 00 00 00 00 00 43 4c 4f 53 45 44 00 cd |V.......CLOSED..|
000000c0 cd cd cd cd 08 00 00 00 49 00 00 00 00 00 00 00 |........I.......|
000000d0 43 4c 4f 53 45 44 00 cd cd cd cd cd 09 00 00 00 |CLOSED..........|
000000e0 53 00 00 00 01 00 00 00 42 6f 62 00 cd cd cd cd |S.......Bob.....|
000000f0 cd cd cd cd 0d 0a 00 00 00 59 00 00 00 00 00 00 |.........Y......|
00000100 00 43 4c 4f 53 45 44 00 cd cd cd cd cd 0b 00 00 |.CLOSED.........|
00000110 00 46 00 00 00 01 00 00 00 4a 69 6c 6c 00 cd cd |.F.......Jill...|
00000120 cd cd cd cd cd 0c 00 00 00 38 00 00 00 00 00 00 |.........8......|
00000130 00 43 4c 4f 53 45 44 00 cd cd cd cd cd 0d 00 00 |.CLOSED.........|
00000140 00 48 00 00 00 00 00 00 00 43 4c 4f 53 45 44 00 |.H.......CLOSED.|
00000150 cd cd cd cd cd 0e 00 00 00 5e 00 00 00 01 00 00 |.........^......|
00000160 00 4a 61 6d 65 73 00 cd cd cd cd cd cd 0f 00 00 |.James..........|
00000170 00 40 00 00 00 01 00 00 00 4a 69 6c 6c 00 cd cd |.@.......Jill...|
00000180 cd cd cd cd cd 10 00 00 00 56 00 00 00 00 00 00 |.........V......|
00000190 00 43 4c 4f 53 45 44 00 cd cd cd cd cd 11 00 00 |.CLOSED.........|
000001a0 00 43 00 00 00 01 00 00 00 4a 61 6d 65 73 00 cd |.C.......James..|
000001b0 cd cd cd cd cd 12 00 00 00 60 00 00 00 01 00 00 |.........`......|
000001c0 00 42 6f 62 00 cd cd cd cd cd cd cd cd 13 00 00 |.Bob............|
000001d0 00 5e 00 00 00 00 00 00 00 43 4c 4f 53 45 44 00 |.^.......CLOSED.|
000001e0 cd cd cd cd cd 14 00 00 00 55 00 00 00 00 00 00 |.........U......|
000001f0 00 43 4c 4f 53 45 44 00 cd cd cd cd cd 15 00 00 |.CLOSED.........|