0

我正在尝试从 .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.........|
4

2 回答 2

0

由于您的代码片段不完整,因此很难准确判断发生了什么,但这里有一些我注意到的事情:

  • 您似乎没有使用读取的 no_of_readings,而是尝试读取 50 条记录。由于您不检查 fread 的返回值,因此您的程序只会继续使用最后一次成功读取的 fread。

  • 如果您不阅读文本,则不应以文本模式打开文件。行尾转换可能会损坏您的数据。改用“rb”(在文本模式下打开的标准 C 方式也只是省略 b,t 是非标准的)。

  • 编辑:在 Windows 上以文本模式读取文件会将 ctrl-z 字节 (26) 解释为文件结尾。这就是您的读取在第 25 条记录后停止的原因。

  • 像这样直接从结构读取/写入数据将导致数据文件不可移植。最好手动将您的结构序列化为字节。

于 2013-03-02T12:06:30.267 回答
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;
       }

这应该可以让您了解出了什么问题

于 2013-03-03T11:32:36.617 回答