Binary IFstream

Started by
6 comments, last by Cibressus 19 years, 7 months ago
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.
| Member of UBAAG (Unban aftermath Association of Gamedev)
Advertisement
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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?

When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
what do you mean by a externious seekg?
| Member of UBAAG (Unban aftermath Association of Gamedev)
Quote:Original post by Cibressus
what do you mean by a externious seekg?


Unnecessary (and actually, probably incorrect).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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();
| Member of UBAAG (Unban aftermath Association of Gamedev)
Well, does it work?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
not allowed to program after 8 on school nights, keeps me up to late.
| Member of UBAAG (Unban aftermath Association of Gamedev)

This topic is closed to new replies.

Advertisement