为什么没有FREAD读取正确的整数? [英] Why isn't fread reading in the correct integer?

查看:175
本文介绍了为什么没有FREAD读取正确的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是我只想澄清为何无法打印正确的输出(年)正确,下面给出code。

This question was just to clarify why the correct output(year) isn't being printed correctly, given the following code.

有关未来的读者,问题是,fget()函数读出这应该是属于一年字节一个字节,导致今年无法正确打印。

For future readers, the issue was that the fget() function read off a byte that was supposed to be a byte that belonged to year, causing the year to not be printed correctly.

下面是我的code
汽车结构

Here is my code Car structure

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

code写二进制文件

Code to write to binary file

  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);
}

和code在二进制文件中读取

And code to read in a binary file

   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文件读取。(从读此加载/写入文本其中工程)

Here is my data that I am trying to write/read from bin file.(loaded this from the read/write to text which works)

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

在读取的二进制方法,我用这条线code为调试目的

In the read binary method, I use this line of code for debugging purposes

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

应在这两种情况下打印2014年。然而,当我尝试运行code,这就是被印刷

Which should print 2014 in both instances. However when i try running the code, this is what gets printed

1275068423
1811939335

有什么我在我的读取和写入我的方法,是造成今年如此关闭失踪?

Is there something i am missing in my read and my write method that is causing the year to be so off?

推荐答案

您有一个逻辑错误。行

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

读取 FTP 的第一个字符是应该是电流 - &GT的一部分;年。当您执行

reads the first character from ftp that is supposed to be part of the current->year. When you execute

         fread(&current->year, sizeof(int),1,ftp);

您正在阅读的错误数据。如果的sizeof(INT) 4 在你的平台上,你领了 3 从已保存的从数据和一字节的字节保存制作

you are reading the wrong data. If sizeof(int) is 4 in your platform, you are picking up 3 bytes of data from the saved year and one byte from the saved 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.

这篇关于为什么没有FREAD读取正确的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆