_eof_eof

测试文件尾 (EOF)。Tests for end of file (EOF).

语法Syntax

int _eof(
   int fd
);

参数Parameters

fdfd
引用打开的文件的文件说明符。File descriptor referring to the open file.

返回值Return Value

如果当前位置是文件尾,则 _eof返回 1; 如果不是,则返回0。_eof returns 1 if the current position is end of file, or 0 if it is not. 返回值-1 指示错误;在这种情况下,将调用无效参数处理程序,如参数验证中所述。A return value of -1 indicates an error; in this case, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则将errno设置为ebadf ( ,指示无效的文件描述符。If execution is allowed to continue, errno is set to EBADF, which indicates an invalid file descriptor.

备注Remarks

_Eof函数确定是否已到达与fd关联的文件的结尾。The _eof function determines whether the end of the file associated with fd has been reached.

要求Requirements

函数Function 必需的标头Required header 可选标头Optional header
_eof_eof <io.h><io.h> <errno.h><errno.h>

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.

示例Example

// 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.txtInput: crt_eof.txt

This file contains some text.

OutputOutput

Number of bytes read = 29

请参阅See also

错误处理Error Handling
低级别 I/OLow-Level I/O
clearerrclearerr
feoffeof
ferrorferror
perror、_wperrorperror, _wperror