Connecting Tech Pros Worldwide Help | Site Map

istream acting weird on windows

Member
 
Join Date: Jun 2007
Posts: 89
#1: Jul 17 '07
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:

Expand|Select|Wrap|Line Numbers
  1. try {
  2.     file.exceptions (ios_base::badbit);
  3.     file.getline (buffer, MAX_BUFFER_SIZE - 1);
  4.     while (!file.eof () && !m_validation_func (buffer, num_fields))
  5.         file.getline (buffer, MAX_BUFFER_SIZE - 1);
  6.  
  7.     // Some code to read the data from 'buffer;
  8.     if (file.eof ())
  9.         return (false);
  10.  
  11.     // Seek the last valid line in the file
  12.     file.seekg (-1, ios::end);
  13.     char c = file.peek ();
  14. }
  15. catch (...) {
  16. if (file.fail ())
  17.     printf ("FAIL!\n");
  18. if (file.bad ())
  19.     printf ("BAD!\n");
  20. if (file.eof ())
  21.     printf ("EOF!\n);
  22. }
  23.  
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?
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#2: Jul 17 '07

re: istream acting weird on windows


You might try reading the file into an array of strings and then reading the array from the last element to the first.

Also, your try block covers too much code. Reduce the number of lines you want to try.

Also, bad(), eof() and fail() are not exception conditions. No exception is thrown on these.

STL throws exception objects. That means you can catch the object and call the what() method to see what the problem is:
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.      //something
  4. }
  5. catch (exception& ex)
  6. {
  7.     cout << ex.what() << endl;
  8.     return;   //bail out
  9. }
  10.  
Member
 
Join Date: Jun 2007
Posts: 89
#3: Jul 18 '07

re: istream acting weird on windows


By calling file.exceptions (ios_base::badbit); it can throw exceptions. In fact it should only throw when badbit is set. The reason I have this code is because I do not want to read the entire file in. I want to spot check the first and last line to see if its worth reading the rest in. By reading it all in I would kill the memory (5GB file in some cases) and if I read line by line it takes awhile.

Anyhow, my update is I finally got it to work. We use the Intel C++ Compiler v 9.0 which works with MS .NET 2003 (and 2005). Well there are the compile flags "/Qip /Ob2" that are optimizations to inline functionality. It tries to be smart and figure out code it can inline for you. Problem is apparently it messes up ifstream in some cases, in our version. But if we upgrade it works. I love compiler bugs. But its running like a charm now. Thanks for the help.
Reply