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

</>
Copy
#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:

  1. We open example.txt in read mode and check if the file pointer fp is valid.
  2. The fgets() function reads a line from the file into the buffer until it returns NULL.
  3. After the loop, feof(fp) checks whether the end of the file has been reached.
  4. 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

</>
Copy
#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:

  1. We open numbers.txt in read mode and validate the file pointer fp.
  2. The fscanf() function attempts to read an integer from the file and stores it in the variable num.
  3. The loop continues until fscanf() returns EOF, indicating no more data is available.
  4. 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

</>
Copy
#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:

  1. We open data.txt in read mode and ensure the file pointer fp is valid.
  2. The fgetc() function reads one character from the file and assigns it to the variable ch.
  3. The loop continues until fgetc() returns EOF, indicating the end of the file.
  4. Each character is printed immediately using putchar().
  5. 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:

  1. feof() Function: Checks for the EOF after reading the file.
  2. fscanf() Return Value: Uses the return value of fscanf() to determine EOF while reading formatted data.
  3. fgetc() Function: Reads the file character-by-character until EOF is encountered.