Jump to content
  • Advertisement


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


loading a bitmap with ifstream

This topic is 6125 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

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 this post

Link to post
Share on other sites
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 this post

Link to post
Share on other sites
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 this post

Link to post
Share on other sites
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 this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!