Test a stream's end-of-file flag
#include <stdio.h> int feof( FILE* fp );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The feof() function tests the end-of-file flag for the stream specified by fp.
Because the end-of-file flag is set when an input operation attempts to read past the end-of-file, the feof() function detects the end-of-file only after an attempt is made to read beyond the end-of-file. Thus, if a file contains 10 lines, the feof() won't detect the end-of-file after the tenth line is read; it will detect the end-of-file on the next read operation.
0 if the end-of-file flag isn't set, or nonzero if the end-of-file flag is set.
#include <stdio.h> #include <stdlib.h> void process_record( char *buf ) { printf( "%s\n", buf ); } int main( void ) { 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 ); return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |