C feof() Function | Check End-of-File for a File Stream

Introduction

The feof() function in C is a standard library function that checks the end-of-file indicator for the given stream. It is part of the C standard library (stdio.h) and is commonly used to detect the end of a file during input operations.

feof() Function Syntax

The syntax for the feof() function is as follows:

int feof(FILE *stream);

Parameters:

  • stream: A pointer to a FILE object that specifies the stream to be checked.

Returns:

  • The function returns a non-zero value if the end-of-file indicator is set for the specified stream. Otherwise, it returns 0.

Examples

Checking End-of-File for a File

To demonstrate how to use feof() to check the end-of-file indicator for a file, we will write a simple program.

Example

#include <stdio.h>

int main() {
    FILE *file;
    int ch;

    // Open the file for reading
    file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("Error: Could not open file for reading.\n");
        return 1;
    }

    // Read characters until the end of the file
    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
    }

    // Check if EOF indicator is set
    if (feof(file)) {
        printf("\nEnd of file reached.\n");
    }

    // Close the file
    fclose(file);

    return 0;
}

Output (assuming example.txt contains the text "Hello, World!"):

Hello, World!
End of file reached.

Handling End-of-File in a Loop

This example shows how to use feof() in a loop to handle the end-of-file condition explicitly.

Example

#include <stdio.h>

int main() {
    FILE *file;
    int ch;

    // Open the file for reading
    file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("Error: Could not open file for reading.\n");
        return 1;
    }

    // Read and print characters until EOF is reached
    while (1) {
        ch = fgetc(file);
        if (feof(file)) {
            break;
        }
        putchar(ch);
    }

    printf("\nEnd of file reached.\n");

    // Close the file
    fclose(file);

    return 0;
}

Output (assuming example.txt contains the text "Hello, World!"):

Hello, World!
End of file reached.

Real-World Use Case

Reading Lines from a File Until EOF

In real-world applications, the feof() function can be used to read lines from a file until the end of the file is reached.

Example

#include <stdio.h>

int main() {
    FILE *file;
    char line[256];

    // Open the file for reading
    file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("Error: Could not open file for reading.\n");
        return 1;
    }

    // Read and print lines until EOF is reached
    while (fgets(line, sizeof(line), file) != NULL) {
        printf("%s", line);
    }

    // Check if EOF indicator is set
    if (feof(file)) {
        printf("\nEnd of file reached.\n");
    }

    // Close the file
    fclose(file);

    return 0;
}

Output (assuming example.txt contains multiple lines of text):

First line
Second line
Third line
End of file reached.

Conclusion

The feof() function is useful for determining whether the end of a file has been reached during reading operations. It helps in managing input operations by allowing you to handle EOF conditions appropriately.

Comments