feof

测试流的文件尾。

语法

int feof(
   FILE *stream
);

参数

stream
指向 FILE 结构的指针。

返回值

如果读取操作已尝试读取超过文件的末尾,feof 函数将返回非零值;否则该函数返回 0。 如果流指针为 NULL,该函数会调用无效参数处理程序,如参数验证中所述。 如果允许执行继续,errno 将设置为 EINVALfeof 将返回 0。

有关返回代码的详细信息,请参阅 errno_doserrno_sys_errlist_sys_nerr

备注

feof 例程(同时作为函数和宏实现)确定是否已超过 stream 的末尾。 当超过文件尾时,读取操作将返回文件尾指示符,直到关闭了流或者针对流调用了 rewindfsetposfseekclearerr 调用。

例如,如果某个文件包含 10 个字节,而你从该文件中读取了 10 个字节,feof 将返回 0,因为即使文件指针位于该文件的末尾,你也未尝试超出末尾读取内容。 当您尝试读取第 11 个字节后,feof 将返回一个非零值。

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

要求

函数 必需的标头
feof <stdio.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_feof.c
// This program uses feof to indicate when
// it reaches the end of the file CRT_FEOF.TXT. It also
// checks for errors with ferror.
//

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
   int  count, total = 0;
   char buffer[100];
   FILE *stream;

   fopen_s( &stream, "crt_feof.txt", "r" );
   if( stream == NULL )
      exit( 1 );

   // Cycle until end of file reached:
   while( !feof( stream ) )
   {
      // Attempt to read in 100 bytes:
      count = fread( buffer, sizeof( char ), 100, stream );
      if( ferror( stream ) )      {
         perror( "Read error" );
         break;
      }

      // Total up actual bytes read
      total += count;
   }
   printf( "Number of bytes read = %d\n", total );
   fclose( stream );
}

输入:crt_feof.txt

Line one.
Line two.

输出

Number of bytes read = 19

另请参阅

错误处理
流 I/O
clearerr
_eof
ferror
perror_wperror