我的!feof(fp)为什么读不到文件尾?
void load(void)
{
FILE *fp;
if((fp=fopen("abc,txt","rb+"))==NULL) {
printf("cannot open file\n");
getch();
return;
}
while(head) {
p1=head->next;
free(head);
head=p1;
}
head=p1=NULL;
while(!feof(fp)) {
p1=(struct student *)malloc(sizeof(struct student ));
if(!p1) {
printf("out of memery\n");
return;
}
if(fread(p1,sizeof(struct student),1,fp)!=1) {
printf("file read error\n");
free(p1);
getch();
return;
}
p2->next=p1;
p2=p1;
if(n==0)
head=p1;
}
p2->next=NULL;
printf("\nload successful\n");
getch();
fclose(fp);
}
假设我的文件里有二项数据,循环二次之后fp应该是到了文件尾,可就是退不出循环。这是怎么回事?
我把fread条件判断去掉,程序循环三次后退出了,为什么会多读了一项?应该怎样改才行。
问题点数:50、回复次数:2Top
1 楼blre(真的无所谓)回复于 2001-05-31 00:10:00 得分 25
第三次的结果是有效的吗?
也可以使用fread的返回值判断Top
2 楼liuto(B34)回复于 2001-05-31 00:18:00 得分 25
应该是三次,第三次其实没读到,因为前两次读完正好读了所有的数据,但feof只有到你读了超出文件的数据时才会变成true.
前一个问题你没说清楚,你跟踪了?怎么知道没退出?到了peintf("file read error")一句了么?Top




