In C++, eof() returns true when the stream has set eofbit. The bit is set when a read tried to go past the last character of the input, whether the input is a file, a string stream, or the keyboard.
The word is short for end of file. A key detail: the bit is set only after a read hits the end, not when you are standing right before it. Include <iostream> to use it.
This is a member function of std::ios. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.
Syntax
Call eof on a stream, usually after a loop has finished. It takes no arguments.
C++
while (std::getline(in, line)) {
// use line
}
if (in.eof()) {
std::cout << "reached the end\n";
}
Call
What it does
in.eof()
Returns true when eofbit is set.
while (in >> x)
The correct way to read until the end. It tests each read as it happens.
in.clear()
Clears eofbit so the stream can be used again.
Use eof() to find out why a loop stopped. Do not use it as the loop condition itself.
Library declarations
C++
bool eof() const;
Parameters
Parameter
Description
None
eof() takes no parameters.
Return Value
Return Value
Description
true
eofbit is set. A read reached the end of the input.
false
The end has not been hit yet. There may still be no more data, but no read has noticed.
C++ Examples
Example 1 — eof() After Reading the Last Value
Read a single number. The stream reaches the end while looking for more digits.
C++
#include <iostream>
#include <sstream>
int main() {
std::istringstream in("5");
int a = 0;
int b = 0;
in >> a;
std::cout << "eof after first read: " << (in.eof() ? "yes" : "no") << '\n';
in >> b;
std::cout << "a = " << a << ", b = " << b << '\n';
std::cout << "fail after second read: " << (in.fail() ? "yes" : "no") << '\n';
return 0;
}
Output
eof after first read: yes
a = 5, b = 0
fail after second read: yes
How It Works
in >> a reads 5. To know the number is finished, it looks at the next character and finds the end of the text.
That look sets eofbit, so eof() is already true even though the read succeeded.
The second read has nothing to read, so it sets failbit. b stays 0 and fail() is true.
Example 2 — Read All Lines, Then Check eof()
The correct loop reads in the condition. Afterwards, eof() explains why it stopped.
std::getline(in, line) returns the stream. As a condition, it is true while the line was read successfully.
The third call finds no characters at all. It sets eofbit and failbit together, so the condition is false and the loop ends.
After the loop, eof() confirms that the end of the text was the reason for stopping.
Example 3 — Why while (!eof()) Is Wrong
The text ends with a space. The wrong loop prints the last number twice. The right loop does not.
C++
#include <iostream>
#include <sstream>
int main() {
std::istringstream bad("1 2 ");
int n = 0;
std::cout << "wrong:";
while (!bad.eof()) {
bad >> n;
std::cout << ' ' << n;
}
std::cout << '\n';
std::istringstream good("1 2 ");
std::cout << "right:";
while (good >> n) {
std::cout << ' ' << n;
}
std::cout << '\n';
return 0;
}
Output
wrong: 1 2 2
right: 1 2
How It Works
In the wrong loop, after 2 is read the stream has only a space left, and eofbit is still not set, so the loop runs again.
The third read skips the space, finds the end, and fails. n keeps its old value 2, which is printed a second time.
In the right loop the read is the condition. A failed read makes the condition false before anything is printed.
Frequently Asked Questions
eof() returns true when a read has reached the end of the input, so eofbit is set. It works for files, string streams, and cin.
eof() becomes true only after a read has tried to go past the end. The loop body therefore runs once more with failed or old data. Use while (file >> x) or while (getline(file, line)) instead.
Press Ctrl+Z then Enter on Windows, or Ctrl+D on Linux and macOS. Then cin.eof() becomes true.
Not always. Reading the last number can set eofbit while the number is still read correctly. A read that finds no data at all sets failbit as well.
Call clear() to remove eofbit, then use seekg(0) to go back to the start. After that the stream can be read from the beginning.