public member function
<ios> <iostream>

std::basic_ios::eof

bool eof() const;
Check whether eofbit is set
Returns true if the eofbit error state flag is set for the stream.

This flag is set by all standard input operations when the End-of-File is reached in the sequence associated with the stream.

Note that the value returned by this function depends on the last operation performed on the stream (and not on the next).

Operations that attempt to read at the End-of-File fail, and thus both the eofbit and the failbit end up set. This function can be used to check whether the failure is due to reaching the End-of-File or to some other reason.

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.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// ios::eof example
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream

int main () {

  std::ifstream is("example.txt");   // open file

  char c;
  while (is.get(c))                  // loop getting single characters
    std::cout << c;

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

  is.close();                        // close file

  return 0;
}


Data races

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

Exception safety

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

See also