Link to home
Start Free TrialLog in
Avatar of Edrd
EdrdFlag for Mexico

asked on

Can you help me to compile this C++ code to read JPG header file?

I need to writte a program. This program should be able to read the file's header (bits) and then show information about the image. LIke
- width
- height
- bitmap
- etc

I have found a code in the Internet, but I can't compile it. I have tried Visual Studio 2005 C++. The compiler marks an error in this line:
fread( JpegInfoHeader, sizeof(JPEGINFOHEADER), 1, filePtr ) ;
I have some experience using C++, but using files and images it is my first time.
 Sincerely I dont know how to solve this. If you know, please let me know.  I'll apreciate it a lot.
Thank you.

PS: Also I attach another source code, it is another possible solution, but honestly I dont understand the code.
//Based on the info here:
//Firstly, define a struct to store the JPEG header info in:
    unsigned short id ;
    unsigned short width ;
    unsigned short height ;
    unsigned char number_of_component ;
    // etc etc
} ;
  * Return value:
  *    0   -   Successful
  *   -1   -   Could not find/open the specified file
  *   -2   -   Specified file is not a valid bitmap file
int LoadJpegHeader( char *filename, JPEGINFOHEADER *jpegInfoHeader )
    FILE *filePtr ;
    filePtr = fopen( filename, "rb" ) ;
    if ( filePtr == NULL )
        return -1 ;
    fread( JpegInfoHeader, sizeof(JPEGINFOHEADER), 1, filePtr ) ;
    fclose( filePtr ) ;
    return 0 ;
int main()
LoadJpegHeader( "image.jpg", jpg );

Open in new window

Avatar of josgood
Flag of United States of America image

Line 35
 fread( jpegInfoHeader, sizeof(JPEGINFOHEADER), 1, filePtr ) ;
note lower case jpegInfoHeader

Line 46
LoadJpegHeader( "image.jpg", &jpg );
Note ampersand preceeding jpb
Avatar of CEHJ
Why has this been posted in the Java TA?

what you should pass to fread is an address, so you should use :

fread(&jpegInfoHeader, sizeof(JPEGINFOHEADER), 1, filePtr ) ;

There is other problem with reading struct as a single block from file.

Depending on your CPU/Compiler/OS, struct could be padded with zero so that internal data are aligned to some boundary (for example, 4 byte boundaries).

So it is more portable to read each items separately or use some kind of #pragma packing (or other non-portable compiler directives)

But at the moment, your problem is just that you are not passing the adress of jpegInfoHeader.

Best regards
Avatar of davidgeldreich
Flag of France image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Edrd


Hi. Thank you very much for your answer. You are an expert! Thank you again. Eduardo