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(...)
 


podoboqAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

itsmeandnobodyelseCommented:
>>>>    bmp >> width;

That statement would work with ASCII numbers but not with binary data. So i think, you have to use ugly read() ...
   
     bmp.read((char*)&width, sizeof(int));

Regards, Alex
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itsmeandnobodyelseCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.