int LoadBitmap8(cBitmap8 &bitmap, char *filename)
{
//open the file
ifstream file(filename, ios::binary);
if(file)
{
//error, file does not exist
return 0;
}
//read in the file header
file.read((char *)&bitmap.m_BMPheader, sizeof(BITMAPFILEHEADER));
//check to see if its a bitmap
if (bitmap.m_BMPheader.bfType != 0x4d42)
{
file.close();
//error
return 0;
}
Anyway,it always fails on the check to see if its a bitmap. I know it is, and thus it has to be in my loader. Can anyone see whats up with it? cBitmap8 is my own class, that has a BITMAPFILEHEADER, BITMAPINFOHEADER, etc.
Thanks for your time,
Chris
Question on my bitmap loader
I''m trying to write my own custom bitmap loader(just to say I can do it, not really any reason). Anyway, I can''t get the data to read properly. Heres the func:
Have a look at your bitmap file (open it as a binary file in Visual Studio). If it doesn''t start with ''BM'' then its not a bitmap. Your code looks OK to me, apart from the "if (file)" line (which I assume is a typo for "if (!file.is_open())"?)
Yes, it does have the BM. I checked it with a confirmed bitmap that worked with another loader. But I figured out the problem.
The if statement *should* read if(!file), not if(file). (a iostream will be false in a boolean operation if the file fails to open). I tried it, and it worked fine. It was strange, as debugin showed that it returned after that statement. Oh well. Thanks for your help,
Chris
The if statement *should* read if(!file), not if(file). (a iostream will be false in a boolean operation if the file fails to open). I tried it, and it worked fine. It was strange, as debugin showed that it returned after that statement. Oh well. Thanks for your help,
Chris
Well, looks like I spoke to soon. I now realize that the function STILL doesn''t work, but its something simpler (I think) this time. The problem is, my stream can''t open the file. I have copies of the bitmap I''m trying to load in every folder I can think of. This is all of them in my project folder. I''m using VC++. Anyway, heres the source:
I''ve even tried calling it with the full path name, i.e "c:\\Program Files\\yada yada" and still no go. Why can''t this open my file??
int LoadBitmap8(cBitmap8 &bitmap, char *filename){ //open the file ifstream file(filename, ios::binary | ios::in); if (!file) { //error, file does not exist return 0; }///function call itself////////////////////////////////////////if (!LoadBitmap8(bitmap8,"goldpaddle.bmp")) { //error return 0;
I''ve even tried calling it with the full path name, i.e "c:\\Program Files\\yada yada" and still no go. Why can''t this open my file??
December 08, 2000 06:47 PM
I don''t know...its a good question, however, I''m fairly new to programming myself so who knows what that says..
December 09, 2000 05:15 AM
You should check whether your file has been opened using the "is_open()" method, not by "if (file)" or "if (!file)".
From my BM loader:
From my BM loader:
// create fstream object and associate with filefstream infile( filename, ios::in | ios::binary );// check that file is openif ( !(infile.is_open()) ){ // couldn''t open file error_log += "ERROR: C_BitmapImageFile::Load couldn''t open file\n"; // error infile.close(); // close file return 1;} // end if// now load the bitmap file header from the file into the header structureinfile.read( (char*) &bitmapfileheader, sizeof(WIN32BITMAPFILEHEADER) );
I''ve tried to both ways, and it still evaluates to false. Its failing just to OPEN the file. I have no idea why. The bitmap IS in the folder that the source is in. I''ve even tried passing the whole pathname, and it still fails. Can anyone think of any reason why its doing this?
In your call to open the file, set ios::nocreate as well as ios::binary in the mode parameter. Then, if the file does not exist, ios::failbit will be set. Call ios::fail() to check whether this is set. Make sure you''re running the code in the folder where the file is (though using the full path should have fixed that). In Visual Studio, you can set this under Project/Settings/Debug tab/Working Directory.
Its also possible that the file is already opened by another program, for example if you''ve opened it to check whether it''s a valid bitmap file, or maybe you''re opening it twice! (The second time will fail if you do).
In your second code segment, you seem to be calling LoadBitmap8 from within itself. Why?
Dave
Its also possible that the file is already opened by another program, for example if you''ve opened it to check whether it''s a valid bitmap file, or maybe you''re opening it twice! (The second time will fail if you do).
In your second code segment, you seem to be calling LoadBitmap8 from within itself. Why?
Dave
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement