Check When the End of a File is Reached in C
The simplest way to determine when the end of a file (EOF) is reached in C is by using functions like feof()
, checking the return value of input functions such as fscanf()
, or reading character-by-character with fgetc()
until they indicate EOF.
Example 1: Using feof()
Function
In this example, we open a file, read its content line-by-line using fgets()
, and then check if the end of the file is reached using the feof()
function.
main.c
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
char buffer[100];
// Read file line-by-line until NULL is returned
while (fgets(buffer, 100, fp) != NULL) {
printf("%s", buffer);
}
// Check if end-of-file is reached
if (feof(fp)) {
printf("\nReached end of file.\n");
}
fclose(fp);
return 0;
}
Explanation:
- We open
example.txt
in read mode and check if the file pointerfp
is valid. - The
fgets()
function reads a line from the file into thebuffer
until it returnsNULL
. - After the loop,
feof(fp)
checks whether the end of the file has been reached. - If EOF is reached, we print a confirmation message.
Output:
Contents of example.txt
Reached end of file.
Example 2: Using fscanf()
Return Value
In this example, we open a file containing numbers, read each integer using fscanf()
, and use its return value to determine when the EOF is reached.
main.c
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp = fopen("numbers.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
int num;
// Read integers until fscanf returns EOF
while (fscanf(fp, "%d", &num) != EOF) {
printf("%d ", num);
}
printf("\nReached end of file.\n");
fclose(fp);
return 0;
}
Explanation:
- We open
numbers.txt
in read mode and validate the file pointerfp
. - The
fscanf()
function attempts to read an integer from the file and stores it in the variablenum
. - The loop continues until
fscanf()
returnsEOF
, indicating no more data is available. - After reading all numbers, we print a message to indicate that the end of the file has been reached.
Output:
10 20 30 40 50
Reached end of file.
Example 3: Using fgetc()
to Read Character-by-Character
In this example, we open a file and read it character-by-character using fgetc()
until the function returns EOF
.
main.c
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp = fopen("data.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
int ch;
// Read the file one character at a time until EOF is reached
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
printf("\nReached end of file.\n");
fclose(fp);
return 0;
}
Explanation:
- We open
data.txt
in read mode and ensure the file pointerfp
is valid. - The
fgetc()
function reads one character from the file and assigns it to the variablech
. - The loop continues until
fgetc()
returnsEOF
, indicating the end of the file. - Each character is printed immediately using
putchar()
. - After the loop, a message is printed to indicate that EOF has been reached.
Output:
[Contents of data.txt]
Reached end of file.
Conclusion
In this tutorial, we explored various methods to check for the end-of-file in C:
- feof() Function: Checks for the EOF after reading the file.
- fscanf() Return Value: Uses the return value of
fscanf()
to determine EOF while reading formatted data. - fgetc() Function: Reads the file character-by-character until EOF is encountered.