std::getline
throws exception when it gets an eof
.
this is how I am doing.
std::ifstream stream;
stream.exceptions(std::ifstream::failbit|std::ifstream::badbit);
try{
stream.open(_file.c_str(), std::ios_base::in);
}catch(std::ifstream::failure e){
std::cout << "Failed to open file " << _file.c_str() << " for reading" << std::endl;
}
while(!stream.eof()){
std::string buffer = "";
std::getline(stream, buffer);
//process buffer
//I do also need to maintain state while parsing
}
In the above code getline
is throwing exception as it gets eof
How to handle this situation ?
EDIT
std::string buffer = "";
while(std::getline(stream, buffer)){
//also causes getline to hit eof and throw
}
eof()
immediately after thestd::getline()
. – Behaviorismeof
, the stream is in abad
state. – Gummous