#### Archived

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

## Recommended Posts

qx452    122
ifstream fin("1.bmp"); char c; while (!fin.eof()) { fin.get(c); cout << c; } It should read the whole file but it always stops at a certain character (char 0 i think but im not sure). Why would that happen?

##### Share on other sites
Kylotan    10007
Because you're reading in text mode, and text mode treats a certain character (I thought it was 255 rather than 0, but it's not really important) as the End Of File character. You may also find that it mangles your bitmap when it reads certain numbers too. Switch to binary mode (look up ios::binary or some flag like that) and your problems will go away.

Edited by - Kylotan on December 17, 2001 9:39:48 PM

##### Share on other sites
qx452    122
That did it. Now why didn''t I think of that... Thanks.

##### Share on other sites
Big Sassy    311
It took me a month to figure that out when I was working on my bitmap loader. I still kick my self for that.

##### Share on other sites
ANSI2000    122
One thign though that I have noticed is...

That alot ppl who write actual c++ classes to load there images use the standard c functions instead of using ifstream... Why is so? Preference?

Edited by - ANSI2000 on December 18, 2001 1:52:58 AM

##### Share on other sites
Kylotan    10007
As with many of the new C++ features (where ''new'' means Not In C), they are powerful, but hard to use properly. For instance, that loop above calling get() is not only reasonably slow, but is also subtly incorrect.

Why? Because eof() is only true after you have attempted to read from the stream and failed. Therefore an invalid character will be passed to cout before the loop terminates. One ''proper'' way to do it would be like so:
  ifstream fin("1.bmp");char c;// Read the character in, but also check the stream state// if the read failed, the loop will terminate here, as// get() returns the stream state.while (fin.get(c)){ cout << c;}

Note that the code is shorter: a nice example of where it doesn''t always take more typing to get the right result.

Of course, I should point out that I''ve seen C programmers make similar (and occasionally heinous) mistakes with the feof() function.

There is also a performance issue: sometimes reading with iostreams is slower than with traditional stdio functions. However, this difference is reduced to often negligible levels if you use the read/readsome functions and load data in larger chunks.

##### Share on other sites
KwamiMatrix    122
Wow, is a bitmap loader using ifstream that short? I don't understand it. In my class Header file for my bitmap loader, I used four different I/0 techniques, and still could not get a bitmap loaded int a direct draw surface. I have ditched direct draw and direct3d for opengl, and have yet to test my bitmap loader with opengl:

Those are my four I/O techniques. As you can see, I made the C I/O technique part of the name of my class prototypes

Edem Attiogbe

Edited by - KwamiMatrix on December 19, 2001 3:41:07 AM