免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1976 | 回复: 4
打印 上一主题 下一主题

fgets()问题? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-28 16:34 |只看该作者 |倒序浏览
我再读取一个文件的时候,为什么最后一句会输出两次?
如下
FILE *nn;
char buffer[256];



while(!feof(nn)){fgets(buffer,256,nn);puts(bufffer);}

论坛徽章:
0
2 [报告]
发表于 2006-05-28 17:28 |只看该作者
很多地方对feof的解释是错误的。有些解释是对的,但例子是错误的。
feof只是对已经读过的内容来判断文件是否结束,因此,
while(!feof(nn)){...}这种用法实际上是错误的!
您还没有读,怎么判断文件是否结束?
正确的用法是:

while (1)
{
   fgets(...);
   if (feof(nn))
   {
      break;
   }
   puts(...);
}

论坛徽章:
0
3 [报告]
发表于 2006-05-29 09:10 |只看该作者
太谢谢了

论坛徽章:
0
4 [报告]
发表于 2006-05-29 22:11 |只看该作者
正对feof()疑惑...感谢...

论坛徽章:
0
5 [报告]
发表于 2006-05-30 13:54 |只看该作者
Tests whether the file position is at the end

#include <stdio.h>
int feof ( FILE *fp  );



The feof( ) macro tests whether the file position indicator of a given file is at the end of the file.

The feof( ) macro's argument is a FILE pointer. One attribute of the file or stream referenced by this pointer is the end-of-file flag, which indicates that the program has attempted to read past the end of the file. The feof( ) macro tests the end-of-file flag and returns a nonzero value if the flag is set. If not, feof( ) returns 0.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP