Tests for end-of-file.
#include <io.h> | Required only for function declarations |
int _eof( int handle );
handle | Handle referring to open file |
The _eof function determines whether the end of the file associated with handle has been reached.
The _eof function returns the value 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, indicating an invalid file handle.
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
clearerr, feof, ferror, perror
/* 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>
void main( void )
{
int fh, count, total = 0;
char buf[10];
if( (fh = _open( "_eof.c", _O_RDONLY )) == - 1 )
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 up actual bytes read */
total += count;
}
printf( "Number of bytes read = %d\n", total );
_close( fh );
}
Number of bytes read = 715