loading a bitmap with ifstream

Started by
6 comments, last by qx452 22 years, 4 months ago
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?
Advertisement
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
That did it. Now why didn''t I think of that... Thanks.
It took me a month to figure that out when I was working on my bitmap loader. I still kick my self for that.
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
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.
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:

void LoadBMP_CreateFile(HWND handle,char *buffer);
void LoadBMP_OpenFile(HWND handle,char *buffer);
void LoadBMP_fopen(HWND handle,char *buffer);
void LoadBMP_creat(HWND handle,char *buffer);


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
Edem Attiogbe
There is more to loading a bitmap than we posted above... I was just demonstrating how to read characters in. You still have to put it into the structure (and potentially calculate stuff, depending on how it was stored).

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

This topic is closed to new replies.

Advertisement