[Previous]
[Contents]
[Next]

eof()

determine if the end-of-file has been reached

Synopsis:

#include <unistd.h>
int eof( int filedes );

Description:

The eof() function determines, at the operating system level, if the end of the file has been reached for the file whose file descriptor is given by filedes. Because the current file position is set following an input operation, the eof() function can be called to detect the end of the file before an input operation beyond the end of the file is attempted.

Returns:

1
the current file position is at the end of the file
0
the current file position isn't at the end
-1
an error occurred; in this case errno is set to indicate the error.

Errors:

EBADF
The filedes argument isn't a valid file descriptor.
ESPIPE
The file is a pipe.
EINVAL
The file is a character device.

Examples:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

void main()
  {
    int filedes , len;
    char buffer[100];

    filedes = open( "file", O_RDONLY );
    if( filedes != -1 ) {
      while( ! eof( filedes ) ) {
        len = read( filedes , buffer, sizeof(buffer) - 1 );
        buffer[ len ] = '\0';
        printf( "%s", buffer );
      }
      close( filedes );
    }
  }

Classification:

WATCOM

Safety:
Interrupt handler No
Signal handler Yes, but modifies errno
Thread Yes

See also:

errno, read()


[Previous]
[Contents]
[Next]