[Previous]
[Contents]
[Next]

feof()

test the end-of-file indicator

Synopsis:

#include <stdio.h>
int feof( FILE *fp );

Description:

The feof() function tests the end-of-file indicator for the stream pointed to by fp. Because this indicator is set when an input operation attempts to read past the end of the file, the feof() function detects the end of the file only after an attempt is made to read beyond the end of the file. Thus, if a file contains 10 lines, the feof() won't detect the end of the file after the tenth line is read; it will detect the end of the file once the program attempts to read more data.

Returns:

0
the end-of-file indicator isn't set for fp.
Nonzero
the end-of-file indicator is set.

Examples:

#include <stdio.h>

void main()
  {
    FILE *fp;
    char buffer[100];

    fp = fopen( "file", "r" );
    fgets( buffer, sizeof( buffer ), fp );
    while( ! feof( fp ) ) {
      process_record( buffer );
      fgets( buffer, sizeof( buffer ), fp );
    }
    fclose( fp );
  }

void process_record( char *buf )
  {
    printf( "%s\n", buf );
  }

Classification:

ANSI

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

clearerr(), ferror(), fopen(), freopen(), perror(), read(), strerror()


[Previous]
[Contents]
[Next]