feof函数重读问题

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

 

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

 

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


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


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

 

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

 

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

 

查看是否有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)

 

 

你可能感兴趣的:(函数)