2

Possible Duplicate:
Why does std::fstream set the EOF bit the way it does?

hello

iam loading a binary file using read(...), however, the eof() gets never true so i check for file end using gcount(), which obviosly is plain wrong

how to detect eof() of a binary file properly?

std::ifstream rf;

rf.open(fpath.c_str(), std::ios::in  | std::ios::binary);

while(!rf.eof()) {

    std::string objectName;

    READ_STR_FROM_BINFILE(rf, objectName);
    //macro code : {
    //  size_t len; 
    //  ff.read(reinterpret_cast<char *>(&len), sizeof(len)); 
    //  std::vector<char> v(len); 
    //  ff.read(&v[0], len); 
    //  ss = std::string(v.begin(), v.end());
    //}

    if (rf.gcount() == 0) {
        //if this happens, there is nothing more to read
        //but the strange thing is, .eof() is not true at this point
        break;
    }

    //loading some structures here

}   
Community
  • 1
  • 1
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
  • 2
    See [Why does std::fstream set the EOF bit the way it does?](http://stackoverflow.com/questions/1039667/why-does-stdfstream-set-the-eof-bit-the-way-it-does). – johnsyweb May 27 '11 at 11:46
  • 1
    `eof` **should** work, but note that as soon as any error flag is set, it won’t ever be reached: therefore: don’t test for eof only, test for *any* error flag. – Konrad Rudolph May 27 '11 at 11:46

1 Answers1

2

I've found an article discussing just that.

Pocket Universe
  • 1,428
  • 1
  • 9
  • 9