#### Archived

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

# Whats wrong with this stream code ?

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

## 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;

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;

printf("%s2\n",&buffer[0]);

long numBuffer;

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

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

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

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 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 on other sites

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

peace and (trance) out

Mage

##### 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.

1. 1
2. 2
3. 3
4. 4
frob
14
5. 5

• 16
• 12
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632167
• Total Posts
3004528

×