读书人

关于读取文件的小疑点。

发布时间: 2012-03-26 15:46:56 作者: rapoo

关于读取文件的小问题。。

while(0==feof(fp))
{
p1=(struct std*)malloc(LEN);
p1->next=NULL;
fread(p1,LEN,1,fp);
if(n==0)
{
p2=head=p1;
n=n+1;
}
p2->next=p1;
p2=p1;
n=n+1;

}
当fp指向一个刚新建的文件的时候。。为什么运行这段程序后n=2??按理说应该不进这个while不是吗

[解决办法]
用事实说话
[解决办法]
注意的while条件0 == feof(fp)

还有,不明白执行过程,就设置个断点,单步调试!

[解决办法]
The feof function returns a nonzero value if a read operation has attempted to read past the end of the file; it returns 0 otherwise.
[解决办法]
The feof( ) function determines whether the end of the file associated with stream has been reached.
A nonzero value is returned if the file position indicator is at the end of the file; zero is returned otherwise.

一般用于对 fread(), fgets()之后状态的测试

个人理解你上述程序等价:

do {
}while();

验证:

fopen();
fread();
while(...)
{
...
}

看是否进入循环

读书人网 >C语言

热点推荐