c++ifstreameof

Ifstream - Reset EOF bit


I'm working with ifstream. I read until the EOF bit is set (I need it this way).

Why then doesn't this work?

// IN is ifstream file. CH is char.

if (IN.eof()) {
    IN.seekg(ios::beg);
    IN.clear();

    if (read((char*)&CH, sizeof(CH)))
        cout << "Success.";
    else
        cout << "Not S.";    
}

The read function is never successful. I tried using IN.setstate(ifstream::goodbit) instead of IN.clear(). But it still fails, am I right?


Solution

  • Change your code like this:

    IN.clear();
    IN.seekg(0, ios::beg);