feof函数重读问题

fgets读到文件最后一行的时候(返回不是NULL) ,feof测试为假,再fgets一次(返回为NULL),再feof测试为真。

 

feof是通过判断read返回错误时,才判断流已经到达结尾。

 

在对流操作的时候,应该判断fgets()为NULL


其实正确使用 feof 函数很容易,只要记住一个原则就是了:“先读文件后用 feof 判断”。


foef(stream)针对的是”流“, 用一个indicator(指示)表明文件尾,而feof判断这个指示是否设置,而不是判断当前SEEK数值是否到尾。

 

seek值到尾了对于“流”不一定是是文件尾,因为“流”是会不断增长的。

 

对于红字部分确实有些代码确实如此

 

while(!feof(fp)) printf("This file has EOF/n"); lseek(fp, 0, SEEK_SET);

查看是否有EOF标识符,然后再进行处理。

 

NAME
       clearerr, feof, ferror, fileno - check and reset stream status

SYNOPSIS
       #include <stdio.h>

       void clearerr(FILE *stream);
       int feof(FILE *stream);
       int ferror(FILE *stream);
       int fileno(FILE *stream);

DESCRIPTION
       The function clearerr clears the end-of-file and error indicators for the stream pointed to by stream.

       The  function  feof tests the end-of-file indicator for the stream pointed to by stream, returning non-zero if it is
       set.  The end-of-file indicator can only be cleared by the function clearerr.

       The function ferror tests the error indicator for the stream pointed to by stream, returning non-zero if it is  set.
       The error indicator can only be reset by the clearerr function.

       The function fileno examines the argument stream and returns its integer descriptor.

       For non-locking counterparts, see unlocked_stdio(3).

ERRORS
       These  functions  should not fail and do not set the external variable errno.  (However, in case fileno detects that
       its argument is not a valid stream, it must return -1 and set errno to EBADF.)

CONFORMING TO
       The functions clearerr, feof, and ferror conform to X3.159-1989 (鈥樷€楢NSI C鈥欌€?.

SEE ALSO
       open(2), unlocked_stdio(3), stdio(3)

 

 

posted on 2009-07-10 16:00  SunBo 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/sunyubo/archive/2009/07/10/2282260.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值