C feof() Function

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.

Table of Contents

  1. Introduction
  2. feof() Function Syntax
  3. Examples
    • Checking End-of-File for a File
    • Handling End-of-File in a Loop
  4. Real-World Use Case
  5. Conclusion

Introduction

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.

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 used for managing file input operations in C. It allows you to check the end-of-file indicator for a specified stream, enabling you to handle EOF conditions appropriately. By understanding and using this function, you can implement more robust and reliable file reading routines in your C programs. Always ensure to check the return value of feof() after read operations to handle the end of file correctly.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top