Archived

This topic is now archived and is closed to further replies.

Whats wrong with this stream code ?

This topic is 5307 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, if you read my last post, sorry. It was garbled, and I had the information to hand, I just didn''t read it well enough. Below is a snippet of code to read in a bitmap file via fstream. I have tried this with different bitmaps, and it always fails. 1) The first int of the file should be 19778 (or "BM" in ASCII) My code refuses to read it in as an integer, but I can read it in as char[2]. But why not as integer. 2) I cannot read in long data. All I end up with is the exception bits fail and badbit being set.
std::fstream fs;
	
fs.exceptions(std::ios::badbit|std::ios::failbit);

try	
{	
	fs.open(fn.c_str(),std::ios::in|std::ios::binary);
	fs.setf(std::ios_base::dec);

	char buffer[2];
//	int buffer2;

	fs>>buffer[0] >> buffer[1];
		
	//fs>>buffer2;


	//Read in Header, Needs to be == 19778 

	printf("%s2\n",&buffer[0]);
	//Read in the Rest of the Header Information

	long numBuffer;

	fs>>numBuffer;//bmp->FileData.bfSize;

//	printf("%i\n",numBuffer);//bmp->FileData.bfSize);


}
	catch(std::ios::failure e)
{
	const int test = fs.exceptions();

	if (test&std::ios_base::badbit)
	printf ("BadBit Exception Thrown.\nType = %i\n",test);

	if (test&std::ios_base::failbit)
	printf ("failbit Exception Thrown.\nType = %i\n",test);

	if (test&std::ios_base::eofbit)
	printf ("eofbit Exception Thrown.\nType = %i\n",test);
}
	
fs.close();

Can anyone see what is wrong (using DJGPP) Bp.

Share this post


Link to post
Share on other sites
quote:
Original post by Bagpuss
1) The first int of the file should be 19778 (or "BM" in ASCII)
My code refuses to read it in as an integer, but I can read it in as char[2]. But why not as integer.
Because it''s not an integer, but two characters instead? Because you''re trying to read 16 bits into 32 using a stream that automatically reads in the appropriate data size?

quote:
2) I cannot read in long data. All I end up with is the exception bits fail and badbit being set.
Have no idea why you would have this problem.

I should mention that it''s a lot easier to read in an entire bitmap header at once: create a bmHeader struct and then read that in using std::fstream::read.

Share this post


Link to post
Share on other sites
I agreee, it''s a lot easier to use std::fstreamm::read to read in the header information.


BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bmiHeader;

fs.read((char*)&bmfHeader, sizeof(BITMAPFILEHEADER));
fs.read((char*)&bmiHeader, sizeof(BITMAPINFOHEADER));


That will read in all of the header info.


peace and (trance) out

Mage

Share this post


Link to post
Share on other sites
Thanks fo r the replies.
I know it is easier to read it all in one go, but things weren''t working so I broke it down, to see where it was going wrong.

I am using djgpp here, and so had to define my own header structures. Like a fool I just wrote the structs without checking them, and used int, not short for the word fields, and so I was getting more information than I was expecting.

(Hence why the error bits were coming on. I posted here in frustration as I couldn''t see what was happening, then an hour later was kicking myself. Thanks for the answers anyway.)

Bp.

Share this post


Link to post
Share on other sites