feof() Function
The feof()
function in C checks whether the end-of-file indicator for a given stream has been set. This function is particularly useful when reading from files, as it allows you to determine if an attempt to read past the end of a file has occurred.
Syntax of feof()
int feof(FILE *stream);
Parameters
Parameter | Description |
---|---|
stream | Pointer to a FILE object that identifies the stream. |
It is important to understand that the end-of-file indicator is set only after an attempt to read at or beyond the end of the file. Moreover, this indicator remains set until it is explicitly cleared using functions such as clearerr()
, rewind()
, fseek()
, fsetpos()
, or freopen()
.
Return Value
The feof()
function returns a nonzero value if the end-of-file indicator for the stream is set, and zero otherwise.
Examples for feof()
Example 1: Basic File Reading with feof()
This example demonstrates how to use feof()
to detect the end of a file while reading its contents.
Program
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "r");
int ch;
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
// Read until the end-of-file indicator is set
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
if (feof(fp)) {
printf("\nReached the end of the file.\n");
}
fclose(fp);
return 0;
}
Explanation:
- A file pointer
fp
is opened for reading from “example.txt”. - The program reads each character using
fgetc()
untilEOF
is encountered. - After the loop,
feof()
is used to check if the end-of-file indicator is set, and an appropriate message is printed. - The file is then closed using
fclose()
.
Program Output:
[Contents of example.txt]
Reached the end of the file.
Example 2: Handling feof() in a Loop with fgetc()
This example highlights the use of feof()
in conjunction with fgetc()
to correctly process file input.
Program
#include <stdio.h>
int main() {
FILE *fp = fopen("data.txt", "r");
int ch;
if (fp == NULL) {
printf("Unable to open file.\n");
return 1;
}
// Process file content
while (1) {
ch = fgetc(fp);
if (feof(fp)) {
break;
}
printf("%c", ch);
}
printf("\nEnd-of-file reached.\n");
fclose(fp);
return 0;
}
Explanation:
- The file “data.txt” is opened for reading.
- A continuous loop is used to read characters one by one using
fgetc()
. - After each read,
feof()
checks if the end-of-file has been reached, breaking the loop if true. - The program prints each character until the end-of-file is detected, then outputs a concluding message.
Program Output:
[Contents of data.txt]
End-of-file reached.
Example 3: Using feof() After a Read Operation
This example illustrates the scenario where feof()
is checked after a read operation, demonstrating that the end-of-file indicator is not set until an attempt is made to read beyond the file.
Program
#include <stdio.h>
int main() {
FILE *fp = fopen("sample.txt", "r");
int ch;
if (fp == NULL) {
printf("Failed to open sample.txt.\n");
return 1;
}
// Read one character past the end-of-file
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
// Now check for end-of-file
if (feof(fp)) {
printf("\nReached end-of-file after reading.\n");
}
fclose(fp);
return 0;
}
Explanation:
- The file “sample.txt” is opened for reading.
- The program reads all characters using
fgetc()
until it returnsEOF
. - After the reading loop,
feof()
is used to verify that the end-of-file indicator is set. - A message is printed indicating that the end-of-file has been reached.
Program Output:
[Contents of sample.txt]
Reached end-of-file after reading.