test the end-of-file indicator
#include <stdio.h> int feof( FILE *fp );
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.
#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 ); }
ANSI
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
clearerr(), ferror(), fopen(), freopen(), perror(), read(), strerror()