feof

測試資料流的檔案結尾。

語法

int feof(
   FILE *stream
);

參數

stream
FILE 結構的指標。

傳回值

如果讀取作業已嘗試讀取超過檔案結尾,則 feof 函式會傳回非零值,否則會傳回 0。 如果資料流程指標為 NULL ,函式會叫用不正確參數處理常式,如參數驗證 中所述 。 若允許繼續執行,errno 會設為 EINVAL,且 feof 會傳回 0。

如需傳回碼的詳細資訊,請參閱 errno_doserrno_sys_errlist_sys_nerr

備註

feof 常式 (實作為函式和巨集) 會判斷是否已超過 stream 的結尾。 超過檔案結尾時,讀取作業會傳回檔案結尾指標直到資料流關閉,或者直到針對它呼叫 rewindfsetposfseekclearerr

例如,如果檔案包含 10 個位元組,而且您從檔案讀取 10 個位元組, feof 則會傳回 0,因為即使檔案指標位於檔案結尾,您也沒有嘗試讀取超過結尾。 只有在您嘗試讀取第 11 個位元組之後,feof 才會傳回非零值。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

需求

函式 必要的標頭
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 );
}

輸入:crt_feof.txt

Line one.
Line two.

輸出

Number of bytes read = 19

另請參閱

錯誤處理
資料流 I/O
clearerr
_eof
ferror
perror, _wperror