I couldn't understand what is the difference between the feof()
function and EOF
? Both represent the end of file, then how are they different and how would we know where to use what?
Where to use the feof()
function and where to use EOF
?
I couldn't understand what is the difference between the feof()
function and EOF
? Both represent the end of file, then how are they different and how would we know where to use what?
Where to use the feof()
function and where to use EOF
?
feof
and EOF
are defined in <stdio.h>
.
EOF
is a macro that expands to an integer constant expression of type int
with a negative value (usually -1
). It is different from all valid byte values returned by getchar()
, getc()
and fgetc()
. These functions return EOF
if no byte could be read from the stream. The return value must be stored into an int
for the comparison to EOF
to reliably detect the end of file.
Regarding feof()
, the quick answer is:
never use
feof(file)
, always check the return value of all file reading functions to detect the end of stream:
for getchar()
, getc()
or fgetc()
:
int c;
while ((c = getchar()) != EOF) {
// handle the byte read from the file
}
// end of file reached
for fgets()
:
char buf[100];
if (fgets(buf, sizeof buf, file)) {
// a line was read into buf, or the initial portion of
// the current line if longer than 99 bytes.
} else {
// end of file has been reached.
}
for fread()
:
size_t nread = fread(buf, size_of_element, count_of_elements, file);
if (nread > 0) {
// handle the elements read
} else {
// end of file reached
}
for scanf()
and fscanf()
:
int value;
int res = fscanf(file, "%d", &value);
if (res == 1) {
// int value was read from the stream
} else {
// no integer could be read: either input is not an integer or
// end of file was reached.
// read and discard the input line
int c = EOF;
if (res != EOF) {
while ((c = getc(file)) != EOF && c != '\n')
continue;
}
if (c == EOF) {
// end of file reached
} else {
// try and recover from invalid input.
}
}
Regarding scanf()
and fscanf()
: reading a string with %s
is an error as any sufficient long input will cause a buffer overflow and could be used as an exploit. It is recommended to use fgets()
or getline()
to read a line into an array of char
and use sscanf()
to parse its contents carefully, specifying a maximum number of characters to store into the destination array for the %s
and %[
conversions:
char line[100];
char player[20];
char eol;
int score;
while (fgets(line, sizeof line, file)) {
if (sscanf(line, "%19s%d%c", player, &score, &eol) != 3 || eol != '\n') {
fprintf(stderr, "invalid input: %s\n", line);
if (!strchr(line, '\n')) {
int c;
while ((c = getc(file)) != EOF && c != '\n')
continue;
if (c == EOF)
break;
}
continue;
}
// handle the player score...
}
// end of file has been reached.
You may want to read and understand Why is “while( !feof(file) )” always wrong? .
The long answer is:
the
feof()
function returns the value of the end-of-file indicator that may have been set by a previous file read operation upon reaching the end of file. Since you should always test for success of these file reading operations, this function is very rarely used, only to distinguish file read errors from reaching the end of file. File read errors are quite rare on desktop systems today so this distinction is usually unnecessary. 99% of code that usesfeof()
is misusing this function.
Function feof(file)
returns TRUE if the previous file operation has set the end of file flag.
EOF
is an integer constant returned by many file I/O functions indicating that it is not possible to read more data from the file/stream.
Where to use feof() function and where to use of?
Basically you can use feof(file)
after I/O operation to check if the end of file flag is set (ie if it is possible to read more data from the file/stream)
EOF is returned by many I/O functions - you need to read the function documentation. Example fscanf
What is the difference between
feof()
function andEOF
... ?
EOF
is returned from various input functions to indicate:
A read end-of-file condition just occurred when attempting to read with no other input. This sets the stream's end-of-file flag.
The stream's end-of-file flag is set and no read attempt occurred.
The stream detects an input error like trying to read from an output stream or (rarely) some communication error.
Other*1.
feof()
reports the stream's end-of-file flag.
Where to use the
feof()
function and where to useEOF
?
Watch for a return value from an input function of EOF
to detect a no-input condition.
Use feof()
to detect why an input function returned EOF
.
int ch = fgetc(stream);
if (ch == EOF) {
if (feof(stream)) puts("End of file");
else puts("Input error");
}
*1 EOF
is also returned on rare machines where sizeof(int) == 1
and can indicate a valid input character. Details here omitted for such a machine.