This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

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


Dear libstdc++ developers,

forgive my ignorance, I can't get a very simple program to run, maybe 
the problem is with libstdc++?

Below is a simple program, it compiles and runs perfectly well on 
Visual C++ versions 5 and 6. All it does is open binary image file, read
couple integers (2 bytes each, short integers), then read the rest
of the file counting the number of pixels (they are unsigned short
integers). It compiles OK, but when I run it it gives:

[lesha@Unicorn read1]$ g++  -o x main.cpp
[lesha@Unicorn read1]$ ./x
File opened OK
 
File size: -1 bytes
Fail bit set
Bad bit or hardfail bit set
Rows:       -4
Cols:       -4
 
... and then I have to Ctrl-C to stop it.

I am using egcs-1.1.2 and libstdc++.so.2.8.0.
Also, I tried  libstdc++-2-libc6.1-1-2.9.0.so, it compiles, but then
generates segmentation violation.

Any suggestions would be appreciated!

Sincerely,

Aleksey

Here's the program
------------------  

#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>

int main ()
{
	ifstream InFile("/home/lesha/Anbar/read1/alex1.dat", ios::binary | ios::nocreate);
	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;
	while ( !InFile.eof()) {
		InFile.read( (char*)&pixel, 2);
		PixelCount++;
	}
	
	cout << endl << "Pixel count: " << PixelCount << endl;	
	return 0;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]