我正在尝试从二进制文件中读取学生记录(SREC)。该文件肯定包含在l完整记录(前一次运行写入文件)。在我的输出中,我得到两次记录的“ERROR:无法从文件读取记录”语句两次。顶部的printf(读数记录,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 %d\n", 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);
}
答案 0 :(得分:1)
从表面上看,此代码的错误检测相反:
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))
is always wrong。你应该使用类似的东西:
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条记录。这在代码中留下了很多重复。
答案 1 :(得分:0)
EOF的错误指示器仅在尝试读取时设置。
在您阅读的地方测试成功阅读。
在您的示例中,测试正好读取1条记录。
答案 2 :(得分:0)
来自fread
手册页:
fread()不区分文件结束和错误,并且调用者必须使用feof(3)和ferror(3)来确定发生了什么。
更改您的测试,例如:
while (fread(...) > 0)
// ...
if (ferror(file))
// an error happened