C++ IOS Library - eof



Description

It is used to check whether eofbit is set. This flag is set by all standard input operations when the End-of-File is reached in the sequence associated with the stream.

Declaration

Following is the declaration for eof() const function.

bool eof() const;

Parameters

none

Return Value

true if the stream's eofbit error state flag is set (which signals that the End-of-File has been reached by the last input operation).

False otherwise.

Exceptions

Strong guarantee − if an exception is thrown, there are no changes in the stream.

Data Races

Accesses the stream object.

Concurrent access to the same stream object may cause data races.

Example

In below example explains about eof() const.

#include <iostream>     
#include <fstream>      

int main () {

   std::ifstream is("example.txt");   

   char c;
   while (is.get(c))                  
      std::cout << c;

   if (is.eof())                      
      std::cout << "[EoF reached]\n";
   else
      std::cout << "[error reading]\n";

   is.close();                        

   return 0;
}
ios.htm
Advertisements