Link to home
Start Free TrialLog in
Avatar of podoboq
podoboq

asked on

ifstream::operator>> problem

im trying to read width and height property of a BMP image file like:

    ifstream bmp(file_name.c_str(), ios::binary);
    if(!bmp) throw string("unable to open " + file_name + "!");
    unsigned int width( 0), height(0);  
    bmp.seekg(18);   //move to  width/height position
    bmp >> width;
    bmp >> height;
   
just nothing happens, though reading chars is okay. isnt ifstream::operator>> supposed to deal with ints also(like istream does)? dont want to use the ugly ifstream::read(...)
 


ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Binary files often are read/written using structs. The struct representation in memory must exactly match to that of the binary data (you have to care about alignments; any new member variable should be allocated at a byte offset that is a multiple of 4 or 8 (depending on compiler settings); or you disable alignment by #pragma). Then, you could read data by record rather than by bytes. You also could handle different records or read whole (rest of) file to a byte array.

Regards, Alex