Share via


_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