认识feof的微妙

对于这个函数的返回值,msdn里这么写道:The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file. It returns 0 if the current position is not end of file.

比如我用fgets函数读一个文件,读完文件尾部的字节后,再读的话fgets就会返回NULL,这时你用feof就可以判断是不是文件结束了还是发生了读错误。

这里有一个微妙的地方要注意了,假如你把这个文件全部读完,此时的文件指针已经位于文件的尾部了,但这时候你用feof函数判断是否文件结束,此函数返回的是NULL。只有再做一次读操作后,调用feof函数才会返回非零的值。

你可能感兴趣的:(认识feof的微妙)