feof() function in C
The feof() function in C is a unique function that helps us determine if we have reached the end of a file while reading it. It is handy when we want to read information from a file until we reach the end.
The feof() function takes a unique "file pointer" as input. A file pointer is like a magic pointer that helps us read and write information from a file. We use this pointer to open files and perform operations on them, such as reading or writing.
The feof() function tells us whether we have reached the end of the file or not. It checks if a special marker called the "end-of-file indicator" has been set for the file we are working with. If the indicator is selected, we have read all the information from the file and cannot read anymore.
The syntax for the feof() function:
It has the following syntax:
int feof(FILE *file);
Comparing the feof() function with the real-world example
To better understand how the feof() function works, let's imagine that we have a book and want to read it page by page until we finish it. The feof() function is similar to a bookmark that helps us know when we reach the last page. It checks whether we can turn the page or have reached the end. If we can turn the page, it means we are not at the end yet. However, if we can't turn the page, it indicates that we have reached the end of the book.
Similarly, when we read a file in a computer program, we use the feof() function to check if we can read more information from the file or have reached the end. We typically utilize the feof() process within a loop, repeatedly checking if we have reached the end of the file. If the feof() function returns a value indicating that we have reached the end, we know it's time to stop reading.
This approach allows us to read files efficiently without attempting to read beyond their content. The feof() function acts as a helpful indicator, guiding us to read until we reach the end of the file, ensuring we get all the information.
Example:
Let's take a program to demonstrate the feof() function:
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *filepointer;
filepointer = fopen("feofFunctiontext.txt","r");
if(filepointer==NULL){
// there is some error in opening the given file
printf("Error in opening the file \n");
exit(1);
}
char ch;
while(1){
// feof() function will return a non zero number when it reached the end of file
// it will return zero if it is not at the end of the file
if(feof(filepointer)!=0){
break;
}
// traversing through the file using filepointer
ch=fgetc(filepointer);
// below prints the feof value and its corresponding file character
// here, if feof is zero, we get a character if it is not zero we cannot see a character
// it indicates that it reached the end of the file
printf("feof() value is %d corresponding character in file %c \n" ,feof(filepointer),ch);
}
fclose(filepointer);
return 0;
}
Output:
![feof() function in C](https://static.tutorialandexample.com/c/feof-function-in-c1.png)
Explanation for the above program:
First, we have the #include statements that allow us to use certain functions and libraries in our program. stdio.h is for standard input/output operations, and stdlib.h is for general-purpose functions.
Next, we have the main() function, which is the entry point of our program.
Inside the main() function, we declare a variable called filepointer of type FILE*. This variable will be used to store the file pointer, which is like a unique key that helps us work with a file.
After that, we use the fopen() function to open a file called "feofFunctiontext.txt" in read mode. If an error opens the file (for example, if the file doesn't exist or we don't have permission to access it), we display an error message and exit the program.
Next, we declare a variable ch of type char. This variable will be used to store each character read from the file.
We enter a while loop that will continue indefinitely until we explicitly break out of it.
Inside the loop, we use the feof() function to check if we have reached the end of the file. If feof(filepointer) returns a non-zero value, we have reached the end and break out of the loop using the break statement.
If we have yet to reach the end of the file, we read a character from the file using the fgetc() function and store it in the ch variable.
After that, we print the value returned by feof(filepointer) (which tells us if we are at the end of the file) and the corresponding character we just read from the file. It helps us to understand the behaviour of the feof() function.
Finally, outside the loop, we close the file using the fclose() function to release any associated resources. The file using the fclose() function to release any resources associated with it.
Example:
Let's take a program to demonstrate the alternative to the feof() function:
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *filepointer;
filepointer = fopen("feofFunctiontext.txt","r");
if(filepointer==NULL){
// there is some error in opening the given file.
printf("Error in opening the file \n");
exit(0);
}
char ch;
printf("characters in file \n");
while(1){
// traversing through the file using filepointer.
ch=fgetc(filepointer);
// if filepointer is EOF, it means we reached end of file.
// if we get, Eof we will terminate the while loop.
// it indicates that we reached the end of the file.
if(ch==EOF){
break;
}
// printing the characters of the file using filepointer
printf("%c ",ch);
}
fclose(filepointer);
return 0;
}
Output:
![feof() function in C](https://static.tutorialandexample.com/c/feof-function-in-c2.png)