Sign in to follow this  

Binary IFstream

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

uhm, i'm a bit confused, but i think i've got this right. if i wanted to read in a bitmap file. i'd first get in info and file headers, and ofcourse my stream. ifstream file(FILE_PATH, ios_base::in | ios_base::binary); BITMAPINFOHEADER biHeader; BITMAPFILEHEADER bfHeader; then reset my get pointer file.seekg(0,ios_base::beg); now, read in the file heeader file.read(bfHeader, sizeof(BITMAPFILEHEADER) now advance it to the info header, i get confused here, does it automaticly advance it for me, and is the arguement the size in bytes or what? file.seekg(sizeof(BITMAPINFOHEADER)); read in the info header file.read(bfHeader,sizeof(BITMAPFILEEADER); now then, read it past, wereever it says the bitmap data starts. file.seekg(bfHeader.bOffBits); now allocate space. *data = new char[biHeader.biSizeImage] now read in the data file.read(*data,biHeader.biSizeImage); now close it off file.close(); is this all right, i've spent the past 2 weeks reading about streams, and i think i got everything right.

Share this post


Link to post
Share on other sites
Quote:
Original post by Cibressus
now advance it to the info header, i get confused here, does it automaticly advance it for me, and is the arguement the size in bytes or what?


Yes and yes, so I think you have an extraneous seekg()

Quote:
now then, read it past, wereever it says the bitmap data starts.
file.seekg(bfHeader.bOffBits);


Double-check that both your seekg() call and bfHeader.bOffBits measure offsets from the same statring position - Beginning of the file? End of the BITMAPINFOHEADER structure?

Quote:
*data = new char[biHeader.biSizeImage]


I'd use a std::vector.

Quote:
is this all right, i've spent the past 2 weeks reading about streams, and i think i got everything right.


Looks about right.

Share this post


Link to post
Share on other sites
one other thing, sizeof() is dangerous with one of the bitmap structures, because that structure is padded, and all your reading will get off by, 2 bytes i believe it was... either turn off structure padding with a compiler option (slower, i dont reccomend it) or add up bytes and use that number
hope that helps
-Dan

btw, if your using a std::ifstream std::ios::in is not needed, and... why are you seeking the beggining of the file? it starts there when you open... btw... you did open right?

Share this post


Link to post
Share on other sites
ok, so rev 2 goes like this.

varibles

ifstream file(FILE_PATH, ios_base::in | ios_base::binary);
BITMAPINFOHEADER biHeader;
BITMAPFILEHEADER bfFile;

set my get pointer to the begining of the file.

file.seekg(0,ios_base::beg);

read in the bitmap file header, sizeofstruct is just something i made up.

file.read(bfHeader, SizeofStruct(BITMAPFILEHEADER));

i don't need to advance it cuz it is automaticly done for me. now i read in my biHeader

file.read(biHeader,SizeofStruct(BITMAPINFOHEADER));

allocate data, i should use a vector, but i'd have to look up thats ussage.

*data = new char[biHeader.bSizeImage];

seek past the offset, divide this value by 8 because it's in bits, and seekg takes bytes. and i looked at the diagram, they both start from the begining. it goes

bitmap file header
bitmap info header
data

file.seekg(biHeader.bOffbits/8);

now read in the data

file.read(*data,biHeader.biSizeImage);

edit: forogt to close

file.close();

Share this post


Link to post
Share on other sites

This topic is 4838 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this