Eof C++

Do you know how much data your file contains? There is no precise answer to the question because nobody can be quite sure of it. In such cases, things that are very much known within the files are:

  1. The type of style formatting
  2. The type of data that exists in the file.

As we discussed, the data stored in the file is consistently unknown. What do we do next? Do we start counting the amount of data stored in a file manually, or should we let the computer do the math? Obviously, we are going to make the computer do the calculation.

There is a special functionality in C++ with the name of eof(). When there is no data left to be read or processed from the input file, it will be true, returning a nonzero value, and of course, it will return 0 if false.

Rules of End of file function:

Before reading data from the input stream, always test the “end of file” condition before processing.

  1. Before the loop, use the priming input statement.
  2. At the end of the loop, use the input statement again.

Always use the while loop when you are not aware of the exact figure of data items in the file because we use a loop when we know the exact figure of data items of the file.

First of all, let’s open C++ file in the terminal of Alpine Linux 3.12.0, and now we can start implementing our examples using the following command:

Example # 1:

Let’s take a simple example where we can easily understand the function of eof() when it returns a non-zero value.

In the above example, we have inserted our desired library. Here we are using #include < bits / stdc++.h >. Because it is essentially a header file that contains all of the standard libraries, using this file in programming is a good idea if you want to cut down on time spent doing chores, especially if your rank is time-sensitive.

This also saves time to initialize all of the essential header files. You don’t need to memorize the entire GNU C++ STL for each function you use. And in our driver function, we have initialized and declared a “stringstream ss.” Here we have to check whether this program simply returns true or false. Depending upon the result, we are going to determine whether our stream has reached the end of the file or not.

Assuming that ” stringstream ss ” initially has a garbage value stored in itself, which means there is some data in “ss” left to be read. According to our discussion above, this program should return 0, which means true because of the garbage value present in the stream ” ss. ”

Now we have saved the answer in ” bool isEOF ” as 0 or 1 in terms of true and false, respectively. When we print this ” isEOF, “whatever the value stored in it, it will claim whether we have reached our end of the file or not.

The following commands will be used to compile a C++ program.

$ g++ -o eof eof.cpp

To check whether our stream array has received the end of the file successfully or not, we will have to execute our program using the following command shown below:

$ ./eof

The output of the following program is given below. As we can see, we have successfully executed our eof() function, and our stream has reached the end of the file.

Example # 2:

In this second example, we are going to empty our stream of the garbage value so that the program returns a nonzero value to us, considering the condition true.

In the above example, we have coded the same logic, but there is a difference in this example.

We have used the clear() function to deal with garbage values in this code. The clear () function is a part of the ios and is used to clear a stream’s error state. For example, if a stream file has a state of error that is set to ” eofbit, “then by using the clear() function, we can reset it to no error state.

As per earlier discussion, we know that the initialized “ss” stream also stores the default garbage value. Therefore, we are going to remove the garbage value using ” ss.clear ( ss.eofbit ) “, which is going to set the error state of our previous program from “eofbit” to “goodbit.”

The output of our program will be ” 1,” as shown in the figure, which indicates that we have effectively executed our eof() function, and our stream file has not reached the end of the file

$ ./eof

Example # 3:

In this example, we are going to use the function eof() after opening the stream file. In order to handle stream files, we first have to include ” #include < fstream > ” so that we can manage our file.

Let’s open a text file in the terminal with the following command and add some text.

$ vi test.txt

The text of the file can be seen in the appended image.

After adding some content to it, we have to use “:wq ” to save and close the file. We have to read this compiled C++ program and apply the eof () function to it.

Moving forward, we have to open our file “test.txt” by using the following command, as you can see in the above example. Syntax for accessing the stream file ” std :: ifstream is ( “test.txt” )” .

Using a while loop, we are taking input from the user to see if the stream has reached its end of the file or not. If the user can type “My name is Aqsa Yasin,” there will be no other content left to be read by the eof () function, which means we have reached the end of the file. If even one character is left to be read, the program will display “EoF not reached”; otherwise, “EoF reached”
The two outputs are displayed below for the following program.

The first output shows us that the user successfully typed the full sentence, and the end of the file has been reached.

$ ./eof

The second output describes the scenario where the user could not complete a sentence, which is why the program has not reached its end yet.

$ ./eof


This article’s main goal is to transmit the knowledge of the ios class method’s eof() function. We have used three examples: the first two examples were to make you understand to grasp the knowledge of the function, and in the third example, we tried to demonstrate a level up the practice which is going to be very useful whenever you wish to make use of the ios method and check if the stream has any errors of EOF or not.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.