I want to read from a stream using std::getline inside a for loop.
The stream I mean is a class inherited from the std::basic_iostream.
std::string line;
for(;;){
try{
std::getline( myStreamObj, line );
if( line != "" ){
std::cout << line << std::endl;
}
}
catch( std::ios_base::failure& ex ){
std::cout << ex.what() << std::endl;
}
}
I want also to check against other error conditions like
eofbit failbit badbit
But I am bit confused about it.
If some of the conditions settings these 3 flags is met is any exception thrown like std::ios_base::failure? How to handlse these 3 cases? Do I have to do checkings other ways?
Thanks
AFG
If you want to capture the errors via exceptions you need to set it using ios::exception. Otherwise an exception will not be thrown. You can check out the documentation here: http://www.cplusplus.com/reference/iostream/ios/exceptions/.
You can also explicitly call ios::fail(), ios::bad() or ios::eof(). Docs here: http://www.cplusplus.com/reference/iostream/ios/