为什么不能读取正确的整数?

时间:2014-10-26 05:44:14

标签: c file struct

这个问题只是为了澄清为什么没有正确打印正确的输出(年份),给出以下代码。

对于未来的读者,问题是fget()函数读取了一个字节,该字节应该是属于年份的字节,导致年份无法正确打印。

这是我的代码 汽车结构

      typedef struct carType Car;
struct carType {
   int vehicleID;
   char make[20];
   char model[20];
   int year;
   int mileage;
   double cost;
   Car *next;
};

写入二进制文件的代码

  void writeBinFile(Car *headPointer)
{
     char fileName[20];
     //prompt user for name of textfile to print to 
    scanf("%s", fileName);
    FILE *ftp;
    Car *start =  headPointer->next;
    ftp = fopen(fileName, "wb");
    char separator = '0';
    while(start != NULL)
    {
       //write out 1 cell of data, cell contains 4 bytes
        fwrite(&start->year,sizeof(int), 1, ftp);
        fwrite(start->make,sizeof(char), strlen(start->make), ftp);
        fwrite(&separator, sizeof(char), 1, ftp); 
        fwrite(start->model, sizeof(char), strlen(start->make), ftp);
        fwrite(&separator, sizeof(char), 1, ftp);
        fwrite(&start->cost, sizeof(float), 1, ftp);
        fwrite(&start->mileage, sizeof(float),1,ftp);
        fwrite(&start->vehicleID, sizeof(int), 1, ftp);
        start = start->next;
    }
    fclose(ftp);
}

要读取二进制文件的代码

   void readFromBinFile(Car *headPointer)
{
      char fileName[20];
     //prompt user for name of textfile to print to 
    scanf("%s", fileName);
    FILE *ftp;
    Car *previous =  headPointer;
    ftp = fopen(fileName, "rb");
    Car *current;

    //go until the end of file is reached
    int c;
    while((c = fget(ftp)) != EOF)
    {
            current = (Car *)malloc(sizeof(Car));
            previous->next = current;
             //program receives 1 cell, that cell contains 4 bytes
             fread(&current->year, sizeof(int),1,ftp);
             printf("%d\n",current->year);
             char make[25];
             int count = 0;
             char oneAtATime= 'a';
             while(oneAtATime != '0') 
             { 
                    fread(&oneAtATime, sizeof(char),1,ftp);
                   if(oneAtATime!='0')
                   {
                      make[count] = oneAtATime;
                        count++;
                   }
            } 
            make[count] = 0;
           strcpy(current->make, make);
             char model[25];
              count = 0;
              oneAtATime= 'a';
             while(oneAtATime != '0') 
             { 
                    fread(&oneAtATime, sizeof(char),1,ftp);
                   if(oneAtATime!='0')
                   {
                      model[count] = oneAtATime;
                        count++;
                   }
            } 
            model[count] = 0;
           strcpy(current->model, model);
           fread(&current->cost, sizeof(float),1, ftp);
           fread(&current->mileage, sizeof(int),1,ftp);
           fread(&current->vehicleID, sizeof(int),1,ftp);
         previous = previous->next;
    } 
    fclose(ftp);
} 

这是我试图从bin文件写入/读取的数据。(从读/写加载到有效的文本中)

2014 Toyota Rav4 cost:$40000 mileage:3000, vehicleID:1
2014 Toyota Celica cost:$3220 mileage:2222, vehicleID:3

在读取二进制方法中,我使用这行代码进行调试

   printf("%d\n",current->year);

哪个应该在两个实例中打印2014。但是,当我尝试运行代码时,这就是打印的内容

1275068423
1811939335

我的阅读和写作方法中是否有一些我错过的东西导致今年如此?

1 个答案:

答案 0 :(得分:2)

您有逻辑错误。这条线

while((c = fgetc(ftp)) != EOF) // Assuming you meant to use fgetc not fget

ftp中读取应该属于current->year的第一个字符。执行时

         fread(&current->year, sizeof(int),1,ftp);
你正在阅读错误的数据。如果平台中sizeof(int)4,则您从保存的3中获取year个字节的数据,从保存的make中获取一个字节。

我建议修理它:

int year;
while(1)
{
        if ( fread(&year, sizeof(int),1,ftp) != 1 )
        {
           // No more data to read.
           break;
        }

        current = (Car *)malloc(sizeof(Car));
        previous->next = current;

        current->year = year;

        // Proceed with reading the rest of the data.
        // Make sure you check the return value of every call
        // to `fread` before proceeding to the next line of code.