Advertisement

Binary input into short variables?

Started by March 01, 2003 01:59 PM
0 comments, last by RenegadeKoolAid 21 years, 8 months ago
Hey everyone, Im working on a lossy compression program for school over spring break here (great homework huh), and its quadtree compression of a picture in binary format. But the professor made up his own kind of picture format, with a .[1] extension and the first 4 bytes of the file are the dimensions of the picture followed by the rest of the picture data, where each byte represents a pixel. My dumb question is, how do I get those first 4 bytes (2 for rows, 2 for columns, which is why Im using shorts) into short variables? This is a really dumb question I know, I probably have some lame syntax bug somewhere but heres what I have: short imgSizeRows; short imgSizeCols; fin.open( argv[1], ios::binary ); fin.read( (char*)&imgSizeRows, sizeof(imgSizeRows) ); fin.read( (char*)&imgSizeCols, sizeof(imgSizeCols) ); Where imgSizeRows & imgSizeCols are private variables in the quadtree class. Right after I read in the dimensions I allocate a 2D array for the rest of the pixels and fill in that array, but for some reason the code above I always get 2''s for the two variables Any suggestions?
Ok I opened the file with a hex editor and the first four bytes are: 02 00 02 00, which should be 512 x 512, for the image dimensions. Anyone know why when I read those first 4 bytes in using the above method i get an output of 2''s for the short variables?

This topic is closed to new replies.

Advertisement