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.

int _eof( 
   int fd 


File descriptor referring to open file.

Return Value

_eof returns 1 if the current position is end of file, or 0 if it is not. A return value of –1 indicates an error; in this case, errno is set to EBADF, which indicates an invalid file descriptor.


The _eof function determines whether the end of the file associated with fd has been reached.


Function Required header Optional headers Compatibility
_eof <io.h> <errno.h> 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_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>
int main( void )
   int  fh, count, total = 0;
   char buf[10];
   if( (fh = _open( "crt_eof.txt", _O_RDONLY )) == - 1 )
        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" );
      /* Total actual bytes read */
      total += count;
   printf( "Number of bytes read = %d\n", total );
   _close( fh );

Input: crt_eof.txt

This file contains some text.


Number of bytes read = 29

See Also

Error Handling Routines, Low-level I/O Routines | clearerr | feof | ferror | perror | Run-Time Routines and .NET Framework Equivalents

© 2015 Microsoft