Monday, May 17, 2010

std::getline

I'm implementing a version check at work involving reading from a file and comparing the contents with data from somewhere else. As such I've hade to brush up on some basic file I/O. I quickly stumbled upon the following:

//-error handling
std::ifstream l_file;
l_file.open( "afile" );
std::string l_str;
std::getline( l_file, l_str );
if( std::ios::end != l_file.tellg() )
{
std::cout << "Garbage trailing first line" << std::endl;
}


When running the above code snippet with a file just containing the line "aoeu", tellg() does not indicate that EOF was reached. This does not block me in any way but I am curious as to why this is. I would have expected the istream to be modified so that the next input operation is performed after the delimiter (\n) if there is one or to indicate EOF (here). I should probably check the EOF bit but I still think that tellg() should provide the same info (since it can).

Going to be working a bit more overtime now. Don't really know when I'll get to start on my RoboCup team or play around with Android or Symbian or anything fun. My head always feels like a brick when I come home from work. Sad really.