<fstream.h> does not seem to be working ....

Benjamin Kosnik bkoz@cygnus.com
Tue Jun 1 12:43:00 GMT 1999


I'd need a copy of your data file to test this properly. 

After the changes needed to prevent the loop, I get this with v-3:

File size: -1 bytes
Fail bit set
Rows:       -4
Cols:       -4


Pixel count: 0

Of course, if I had the data set I'd be able to get something different.

Here's some commentary:

// <bkoz00>: I belive MSVC++5/6 ship with two C++ libraries, one an
// older, ARM-style library based on AT&T sources that is mostly
// compatible with v-2, and a licensed version of ISO-conformant code,
// by PJ Plauger. They differentiate between the two via the ".h": the
// old library is in <iostream.h>, the new in <iostream>. You seem to
// be using the older library, which is different than what
// libstdc++-v3 is attempting. Okay?

#include <iostream>
#include <iomanip>
#include <fstream>

int main ()
{
  // <bkoz01>: there is no longer an ios_base::openmode for nocreate,
  // as nocreate is the default. Also, change ios to ios_base
  ifstream InFile("alex1.dat", ios_base::binary | ios_base::trunc);
  if ( InFile.good())
    cout << "File opened OK" << endl;
  
  short int Rows=-4, Cols=-4;
  unsigned short int pixel;
  long PixelCount=0;
  
  //See if file size is read correctly
  InFile.seekg(0, ios::end);
  int end = InFile.tellg();
  cout << endl << "File size: " << end << " bytes";
  InFile.seekg(0, ios::beg);
  
  
  //Read first 4 bytes - number of Columns and Rows
  InFile.read((char*)&Cols, 2);
  InFile.read((char*)&Rows, sizeof(short int));
  
  //Check stream status
  cout << endl;
  if ( InFile.good())
    cout << "File still OK" << endl;
  if ( InFile.eof())
    cout << "EOF bit set" << endl;
  if ( InFile.fail())
    cout << "Fail bit set" << endl;
  if ( InFile.bad())
    cout << "Bad bit or hardfail bit set" << endl;  
  
  
  cout << "Rows: " << setw(8) << Rows << endl;
  cout << "Cols: " << setw(8) << Cols << endl;
  
  cout << endl;
  // <bkoz03> but this is currently in a failed state, as I don't have
  // the data file and thus as the stream is unopened. You should
  // probably just do this: 
  while ( InFile.good()) {
    InFile.read( (char*)&pixel, 2);
    PixelCount++;
  }
  
  cout << endl << "Pixel count: " << PixelCount << endl;  
  return 0;
  
}




More information about the Libstdc++ mailing list