feof() function in C language with Example

Here, we are going to learn about the feof() function of library header stdio.h in C language with its syntax, example.
Submitted by Souvik Saha, on January 06, 2019

feof() function in C

Prototype:

    int feof(FILE* filename);

Parameters:

    FILE *filename

Return type: int(0 or 1)

Use of function:

In C language when we are using a stream that links with a file, then how can we determine we come to the end of a file. To solve the problem we have to compare an integer value to the EOF value. Through the feof() function we determine whether EOF of a file has occurred or not. The prototype of this function is int feof(FILE* filename);

It returns the value zero when end of the file has not occurred, otherwise it returns 1.

feof() example in C

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE* f;
    char str[100];

    //Check the existence of that file
    if ((f = fopen("includehelp.txt", "r")) == NULL) {
        printf("Cannot open the file...");
        //if not exist program is terminated
        exit(1);
    }

    printf("File content is--\n");
    //print the strings until EOF is encountered
    while (!feof(f)) {
        fgets(str, 100, f);
        //print the string
        printf("%s", str);
    }

    //close the opened file
    fclose(f);

    return 0;
}

Output

feof example in c

C stdio.h Library Functions Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.