测试流的文件尾。Tests for end-of-file on a stream.


int feof(
   FILE *stream


指向文件结构的指针。Pointer to FILE structure.

返回值Return Value

如果读取操作已尝试读取超过文件末尾的值, feof函数将返回非零值;否则返回0。The feof function returns a nonzero value if a read operation has attempted to read past the end of the file; it returns 0 otherwise. 如果流指针为NULL,则函数将调用无效参数处理程序,如参数验证中所述。If the stream pointer is NULL, the function invokes the invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,则将errno设置为EINVALfeof将返回0。If execution is allowed to continue, errno is set to EINVAL and the feof returns 0.

有关这些代码以及其他错误代码的详细信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrSee _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.


Feof例程(作为函数和宏实现)确定是否已传递的末尾。The feof routine (implemented both as a function and as a macro) determines whether the end of stream has been passed. 当文件结束时,读取操作将返回文件尾指示符,直到流关闭,或者对其调用fsetposfseekclearerrWhen the end of file is passed, read operations return an end-of-file indicator until the stream is closed or until rewind, fsetpos, fseek, or clearerr is called against it.

例如,如果某个文件包含10个字节,并且您从该文件中读取了10个字节,则feof将返回0,因为即使文件指针位于该文件的末尾,您也不会尝试读取超出末尾的位置。For example, if a file contains 10 bytes and you read 10 bytes from the file, feof will return 0 because, even though the file pointer is at the end of the file, you have not attempted to read beyond the end. 只有在您尝试读取第11个字节后, feof将返回一个非零值。Only after you try to read an 11th byte will feof return a nonzero value.


函数Function 必需的标头Required header
feoffeof <stdio.h><stdio.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


// 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" );

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

输入:crt_feof.txtInput: crt_feof.txt

Line one.
Line two.


Number of bytes read = 19

请参阅See also

错误处理Error Handling
流 I/OStream I/O
perror、_wperrorperror, _wperror