EOF Function in C++
What is eof function?
The end of file is a function in C++ that determines if the file has reached its end. It reads the file till the end of the file. We are unsure about the amount of data stored in the file in file handling. In a file, we only know about the types of data stored in a file and its formatting styles. To know how much data is present in the file, we use eof().
End of File or eof() is a bool-type method of input-output stream (ios) class that returns True on reaching at the end of the file and returns False when there is some data left in the file.
Syntax of the eof() function
bool eof() const;
eof() is a bool method of type constant which indicates we cannot change it.
- bool is a keyword for the Boolean data type
- eof() is the end of file function
- const is the keyword for constant functions or variables.
Parameters:
This function does not contain any parameters.
Return Value:
It returns “TRUE” when the file is at its end and it returns “FALSE” when the file has some more data left and it’s not the end.
Rules of Using End of File Function
- Check for the eof() condition before you process data from the input file stream.
- Before starting the loop, we can use the input statement to check the eof() function.
- At the bottom of the loop, we can repeat the input statement.
- We can get data from an input file stream by using a while loop. (For loop is only used when we know the exact number of data items in the file, in this case, we do not know the exact numbers of data.
Detecting End of File
Detecting the end of file condition is necessary to prevent any further attempts to read data from the file.
To check this condition, we use the while loop:
while(object_name)
Object_name is any ifstream object say fin, which returns 0 if it reaches the end of the file. Thus, the while loop terminates when the fin returns zero on reaching the end-of-file condition. There can also be different reasons for terminating the while loop.
There is another method to detect the end of file condition.
if(finl.eof() != 0) {exit(1); }
This condition will check if the end of file is not equal to zero or 1, and then exit the loop which indicates you have reached the end of the location.
Let’s explore a few examples to understand the concept of eof()
Example 1: Program to use eof() to read an already existing file.
For implementing this code, we have made a text file and saved it in the same folder as the code.
![EOF Function in C++](https://static.tutorialandexample.com/cpp/eof-function-in-cpp1.png)
CODE
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("text.txt");
char f;
while (fin.get(f))
cout << f;
if (fin. eof())
cout << "\n\nReached at the End of the File\n";
else
cout << "Error!\n";
fin.close();
return 0;
}
OUTPUT
![EOF Function in C++](https://static.tutorialandexample.com/cpp/eof-function-in-cpp2.png)
We have used the fstream library to access the file and its object. Then, the text file is read by an object fin made using ifstream. A while loop is run which reads the text file and then a condition will check for the end of file and prints a message.
Example 2: Program to get the data from the file in C++.
For this code, we have made a text file and saved it in the same folder as the code.
![EOF Function in C++](https://static.tutorialandexample.com/cpp/eof-function-in-cpp3.png)
CODE
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char ch[10];
ifstream fin("text1.txt");
if(!fin){
cout<<"Error";
}
cout<<"FILE CONTAINS:\n";
while(!fin.eof())
{
fin>>ch;
cout<<ch<<endl;
}
return 0;
}
OUTPUT
![EOF Function in C++](https://static.tutorialandexample.com/cpp/eof-function-in-cpp4.png)
In this code, the file read and prints the file until the end of the file. It will list all the content of the file.
Example 3: Program to illustrate the use of eof() with multiple files in C++
CODE
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream f_out;
f_out.open("Country");
f_out<< "India\n";
f_out<< "United Kingdom\n";
f_out<< "United States of America\n\n";
f_out.close();
f_out.open("Capital");
f_out<< "New Delhi\n";
f_out<< "London\n";
f_out<< "Washigton\n";
f_out.close();
int n=50;
char line[n];
ifstream f_in;
f_in.open("Country");
cout<<"The contents of Country file:\n";
while(!f_in.eof()) //Using EOF to read the file till its end
{
f_in.getline(line, n);
cout<<line<<endl;
}
f_in.close();
f_in.open("Capital");
cout<<"The contents of Capital file:\n";
while(!f_in.eof()) //Using EOF to read the file till its end
{
f_in.getline(line, n);
cout<<line<<endl;
}
f_in.close();
return 0;
}
OUTPUT
![EOF Function in C++](https://static.tutorialandexample.com/cpp/eof-function-in-cpp5.png)
In this illustration of eof(), we have created two files named Country and Capital. After opening the files, we have written the contents of both files simultaneously. While reading the files, we checked for an end-of-file condition that will read the files until their end and will print their contents.