我正在尝试从.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,但它会跳过一些记录......有什么帮助吗?
这是输出图像
UPDATE(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.........|
答案 0 :(得分:0)
由于您的代码段不完整,很难准确说出发生了什么,但这里有一些我注意到的事情:
您似乎没有使用读入no_of_readings,而是尝试读取50条记录。由于你没有检查fread的返回值,你的程序只会继续使用上次成功读取的内容。
如果您没有阅读文字,则不应在文本模式下打开文件。行结束转换可能会破坏您的数据。改为使用“rb”(在文本模式中打开的标准C方式只是省略b,t是非标准的)。
编辑:在Windows上以文本模式读取文件会将ctrl-z字节(26)解释为文件末尾。这就是为什么你的阅读在第25条记录之后停止的原因。
直接向/从这样的结构中读取/写入数据将导致数据文件不可移植。最好手动将结构序列化为字节。
答案 1 :(得分:0)
我建议检查fread的返回值以检查错误
int size = fread(&my_record,sizeof(struct Oven),1,ptr_myfile);
if (size != sizeof(struct Oven))
{
perror ("Error reading Line %d", counter);
break;
}
这应该可以了解出了什么问题