Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Tests for end-of-file on a stream.

int feof( 
   FILE *stream 


Pointer to FILE structure.

Return Value

The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file. It returns 0 if the current position is not end of file. There is no error return.


The feof routine (implemented both as a function and as a macro) determines whether the end of stream has been reached. When end of file is reached, read operations return an end-of-file indicator until the stream is closed or until rewind, fsetpos, fseek, or clearerr is called against it.


Function Required header Compatibility
feof <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// 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;

   if( (stream = fopen( "crt_feof.txt", "r" )) == NULL )
      exit( 1 );

   /* Cycle until end of file reached: */
   while( !feof( stream ) )
      /* Attempt to read in 10 bytes: */
      count = fread( buffer, sizeof( char ), 100, stream );
      if( ferror( stream ) )      {
         perror( "Read error" );

      /* 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.


Number of bytes read = 19

See Also

Error Handling Routines | Stream I/O Routines | clearerr | _eof | ferror | perror | Run-Time Routines and .NET Framework Equivalents

© 2016 Microsoft