c语言 - fread 从二进制文件读取记录时返回 < 1



我正在尝试从二进制文件中读取学生记录(SREC)。该文件肯定包含至少l个完整记录(上一次运行写入该文件)。在我的输出中,对于两条记录,我得到了两次"ERROR:Cannot read record from file"语句。顶部的printf(Reading record,numR)显示了3次,所以我知道while循环在应该达到eof的时候被输入了3次。

有什么想法吗?提前感谢!

/*Create and populate all 4 lists if lname list exists from previous usage*/
     if((readL = fopen("last", "rb")) == NULL)
     {
        printf("nNew file will be created after server terminates.n");
        created = 1;
     }
     else
        {           
        created = 0;
/*Read in record from binary file*/
        while(!feof(readL))
        {
            numR++;         
printf("Reading record %dn", numR);
            /*Create new temporary SREC*/
            newSREC = (SREC*)malloc(sizeof(SREC));
            newSREC2 = (SREC*)malloc(sizeof(SREC));
            newSREC3 = (SREC*)malloc(sizeof(SREC));
            newSREC4 = (SREC*)malloc(sizeof(SREC));
            /*Read in one record from file*/
            if(fread(newSREC, sizeof(SREC), 1, readL) < 1)
            {
                newSREC2 = newSREC;
                newSREC3 = newSREC;
                newSREC4 = newSREC;
                firstL = insert(newSREC, 1, firstL);
                firstF = insert(newSREC2, 2, firstF);
                firstS = insert(newSREC3, 3, firstS);
                firstG = insert(newSREC4, 4, firstG);
            }
            else
            {
                printf("ERROR: Could not read record from file.n");
            }
        }
        fclose(readL);
    }

曲面问题

从表面上看,这个代码的错误检测颠倒了:

if (fread(newSREC, sizeof(SREC), 1, readL) < 1)
{
    newSREC2 = newSREC;
    newSREC3 = newSREC;
    newSREC4 = newSREC;
    firstL = insert(newSREC, 1, firstL);
    firstF = insert(newSREC2, 2, firstF);
    firstS = insert(newSREC3, 3, firstS);
    firstG = insert(newSREC4, 4, firstG);
}
else
{
    printf("ERROR: Could not read record from file.n");
}

如果读取了完整的记录,则fread()的返回值为1;否则它将为零。您应该使用:

if (fread(newSREC, sizeof(SREC), 1, readL) == 1)

深入挖掘

循环不应该使用feof()——请参阅while (!feof(file))总是错误的。你应该使用这样的东西:

SREC srec;
while ((fread(&srec, sizeof(srec), 1, readL) == 1)
{
    SREC *newSREC1 = malloc(sizeof(*newSREC1));
    SREC *newSREC2 = malloc(sizeof(*newSREC2));
    SREC *newSREC3 = malloc(sizeof(*newSREC3));
    SREC *newSREC4 = malloc(sizeof(*newSREC4));
    if (newSREC1 == 0 || newSREC2 == 0 || newSREC3 == 0 || newSREC4 == 0)
    {
        free(newSREC1);
        free(newSREC2);
        free(newSREC3);
        free(newSREC4);
        …report error…
        break;
    }
    *newSREC1 = srec;
    *newSREC2 = srec;
    *newSREC3 = srec;
    *newSREC4 = srec;
    firstL = insert(newSREC1, 1, firstL);
    firstF = insert(newSREC2, 2, firstF);
    firstS = insert(newSREC3, 3, firstS);
    firstG = insert(newSREC4, 4, firstG);
}
// Optional analysis of why the loop ended — feof() vs ferror() vs short read

代码会泄漏内存,就像从来没有内存短缺一样。在分配了newSREC2等之后,它用指向newSREC1的指针覆盖指针。作业应该是:

*newSREC2 = *newSREC; // Original code — see above for revision

我注意到,您不能在一个数组中分配所有4条记录,因为您几乎肯定需要能够独立释放这4条记录。这在代码中留下了大量重复。

来自fread手册页:

fread()不区分文件结尾和错误,调用方必须使用feof(3)和ferror(3)来确定发生的错误。

将你的测试更改为以下内容:

while (fread(...) > 0)
    // ...
if (ferror(file))
    // an error happened

EOF的错误指示器仅在尝试读取时设置。

在你阅读的地方测试阅读是否成功。

在您的示例中,测试是否只读取1条记录。

相关内容

  • 没有找到相关文章

最新更新