ifstream.fail() behaves not as described?

Hi, i have read today that fail() should return true "when some error other than reaching the End-Of-File occurs": http://www.cplusplus.com/reference/ios/ios/fail/

But actually it behaves in other way for me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fstream>
#include <iostream>
#define FILEPATH "file.txt"
int main() {
  std::ifstream file(FILEPATH);
  if (file.fail())
    return 1;
  char byte;
  while (!file.eof()) {
    std::cout << "gonna read" << std::endl;
    file.read(&byte, 1);
    if (file.eof())
      std::cout << "eof" << std::endl;
    if (file.fail()) {
      std::cout << "fail" << std::endl;
      return 2;
    }
  }
  return 0;
}

Launched it with valid short file:

$ ./ifstream_fail_eof                                                                                   
gonna read
gonna read
gonna read
gonna read
gonna read
gonna read
eof
fail


The point is that fail flag is also being set when eof situation happens!
I used g++ (Gentoo 4.5.3-r2 p1.1, pie-0.4.7) 4.5.3
I'm just frustrated, i don't believe that this is plainly bug of g++, or that documentation on cplusplus.com became incorrect... But also i don't see error from my side.
Any comments?
Each I/O operation has its own requirements for setting the stream status bits. istream::read(), in particular, sets both eofbit and failbit if it fails to read due to the end-of-file condition.

http://en.cppreference.com/w/cpp/io/basic_istream/read
Topic archived. No new replies allowed.