_eof

测试文件尾 (EOF)。

语法

int _eof(
   int fd
);

参数

fd
引用打开的文件的文件说明符。

返回值

如果当前位置是文件尾,则 _eof 返回 1;否则返回 0。 返回值 -1 值表示错误;在此示例中,调用了无效参数处理程序,如参数验证中所述。 如果允许执行继续,则 errno 将设置为指示无效文件说明符的 EBADF

注解

_eof 函数确定是否已到达与 fd 关联的文件的结尾。

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

要求

函数 必需的标头 可选标头
_eof <io.h> <errno.h>

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

示例

// crt_eof.c
// This program reads data from a file
// ten bytes at a time until the end of the
// file is reached or an error is encountered.
//
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <share.h>

int main( void )
{
   int  fh, count, total = 0;
   char buf[10];
   if( _sopen_s( &fh, "crt_eof.txt", _O_RDONLY, _SH_DENYNO, 0 ) )
   {
        perror( "Open failed");
        exit( 1 );
   }
   // Cycle until end of file reached:
   while( !_eof( fh ) )
   {
      // Attempt to read in 10 bytes:
      if( (count = _read( fh, buf, 10 )) == -1 )
      {
         perror( "Read error" );
         break;
      }
      // Total actual bytes read
      total += count;
   }
   printf( "Number of bytes read = %d\n", total );
   _close( fh );
}

输入:crt_eof.txt

This file contains some text.

输出

Number of bytes read = 29

另请参阅

错误处理
低级别 I/O
clearerr
feof
ferror
perror_wperror