int feof(FILE *stream);
checks that are applied to a stream or data or the end-of-file indicator is set.
Used to determine if the end of the file (stream) specified, has been reached. When a file is being read sequentially one line, or one piece of data at a time (by means of a loop, say), this is the function you check every iteration, to see if the end of the file has come.
Returns non-zero if end-of-file indicator is set for stream stream.
/* * feof example code * http://code-reference.com/c/stdio.h/feof */ #include <stdio.h> /* including standard library */ //#include <windows.h> /* uncomment this for Windows */ #define MAX 1000 int main( void ) { char buffer[MAX]; FILE *filestream; filestream = fopen("test.txt","r"); while (feof(filestream) == 0) { fgets(buffer, MAX, filestream); if (buffer[0] == '#') { continue; } else { printf("%s",buffer); } buffer[0]='\0'; } fclose(filestream); return 0; }
content of test.txt
Line 1 #Line 2 Line 3 #Line 4 Line 5
user@host:~$ ./feof Line 1 Line 3 Line 5