mardi 5 février 2019

Is calling clear() on an ifstream problematic?

I have a function that reads from a file with a fixed format in a loop like so:

istream.seekg(0, std::ios::begin);
while (istream >> x >> y) {
  // do something
}

As I discovered and as has been well documented here after the loop exits both eofbit and failbit are set. I want to be able to re-read the file again the next time the function is called without closing and re-opening the ifstream so to get this to work I have to add an istream.clear() first. It seems odd to me that since c++11 the documented behavior of seekg() says it will clear eofbit but it doesn't do anything with failbit which kind of makes this aspect of seekg() seem useless to me. In any case, is calling clear() in any way problematic such as hiding any potential error conditions, etc.?

Aucun commentaire:

Enregistrer un commentaire