I have some code that does two passes in a file. Each pass opens and closes the file itself, so no problem there. The first pass reads the first and last valid lines, whereas the second pass reads the entire file.
The problem is in the first pass. I have the following loop:
-
try {
-
file.exceptions (ios_base::badbit);
-
file.getline (buffer, MAX_BUFFER_SIZE - 1);
-
while (!file.eof () && !m_validation_func (buffer, num_fields))
-
file.getline (buffer, MAX_BUFFER_SIZE - 1);
-
-
// Some code to read the data from 'buffer;
-
if (file.eof ())
-
return (false);
-
-
// Seek the last valid line in the file
-
file.seekg (-1, ios::end);
-
char c = file.peek ();
-
}
-
catch (...) {
-
if (file.fail ())
-
printf ("FAIL!\n");
-
if (file.bad ())
-
printf ("BAD!\n");
-
if (file.eof ())
-
printf ("EOF!\n);
-
}
-
There is more code after this, but it fails here. peek throws an exception. In my catch block I check for bad () and fail () and eof () they are false. I know its the peek since I have some debug printfs in there. To make it even odder, if I compile with debug flags it works properly.
Any ideas whats going on? Also the algorithm is a bit shoddy since it reads character by character. Anyone have a good algorithm for using getline to read a file in reverse order?