Share via


feof

文件尾的测试在流。

int feof( 
   FILE *stream 
);

参数

  • stream
    指向 FILE 结构的指针。

返回值

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

有关这些内容的更多信息以及其他错误代码,请参见 _doserrno、errno、_sys_errlist 和 _sys_nerr

备注

feof 例程 (实现作为函数和宏。) 确定 stream 的结尾是否已通过。 当文件尾时,将读取操作返回文件尾指示符,直到关闭了流或 rewind,fsetpos、fseek或 clearerr 调用它。

例如,在中,如果文件包含 10 字节,并且您阅读 10 字节从文件,feof 将返回 0,因为,因此,即使文件指针位于文件的末尾,则未尝试超出结束。阅读。 在尝试读取之后的第 11 个字节将 feof 返回一个非零值。

要求

功能

必需的标头

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 );
}

Input: crt_feof.txt

Line one.
Line two.

Output

Number of bytes read = 19

.NET Framework 等效项

不适用。若要调用标准 C 函数,请使用 PInvoke。有关更多信息,请参见平台调用示例

请参见

参考

错误处理 (CRT)

流 I/O

clearerr

_eof

ferror

perror、_wperror