Thanks Thantos. Your example was exactly what I was looking for. I've spent entirely too much time being stuck on this little piece of this project. Here is the code snippet for others that may come in my shoes:
Code:
std::ifstream infile(m_szLogfile);
std::string strLine;
char buf;
infile.seekg(0, std::ios::beg);
std::ifstream::pos_type posBeg = infile.tellg();
infile.seekg(-1, std::ios::end);
while (infile.tellg() != posBeg)
{
buf = static_cast <char>(infile.peek());
if (buf != '\n')
{
strLine += buf;
}
else
{
std::reverse(strLine.begin(), strLine.end());
// Do something interesting with the line.
strLine.clear();
}
infile.seekg(-1, std::ios::cur);
}
strLine += static_cast <char>(infile.peek());
std::reverse(strLine.begin(), strLine.end());
// Do something interesting with the line.
infile.close();
Using this technique, I can examine each line as it's being read (starting from the end of the file ) and choose to stop processing the file as soon as I meet some criteria.
Thanks everyone for the other great input.
I'm curious, and this may be OT, but I've always used the namespace std::ios_base for end and beg. When I wrote the above code using that namespace instead of std::ios, it didn't work. What is the main difference between std::ios::beg and std::ios_base::beg?