title: c get data from BMP
body: <p>I find myself writing a simple program to extract data from a bmp file. I just got started and I am at one of those WTF moments.</p>
  
  <p>When I run the program and supply this image: <a href="http://www.hack4fun.org/h4f/sites/default/files/bindump/lena.bmp" rel="nofollow">http://www.hack4fun.org/h4f/sites/default/files/bindump/lena.bmp</a></p>
  
  <p>I get the output:</p>
  
  <pre><code>type: 19778
  size: 12
  res1: 0
  res2: 54
  offset: 2621440
  </code></pre>
  
  <p>The actual image size is 786,486 bytes. Why is my code reporting 12 bytes?</p>
  
  <p>The header format specified in,
  <a href="http://en.wikipedia.org/wiki/BMP_file_format" rel="nofollow">http://en.wikipedia.org/wiki/BMP_file_format</a> matches my BMP_FILE_HEADER structure. So why is it getting filled with wrong information?</p>
  
  <p>The image file doesn't appear to be corrupt and other images are giving equally wrong outputs. What am I missing?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct {
      unsigned short type;
      unsigned int size;
      unsigned short res1;
      unsigned short res2;
      unsigned int offset;
  } BMP_FILE_HEADER;
  
  int main (int args, char ** argv) {
      char *file_name = argv[1];
  
      FILE *fp = fopen(file_name, "rb");
  
      BMP_FILE_HEADER file_header;
  
      fread(&amp;file_header, sizeof(BMP_FILE_HEADER), 1, fp);
  
      if (file_header.type != 'MB') {
          printf("ERROR: not a .bmp");
          return 1;
      }
  
      printf("type: %i\nsize: %i\nres1: %i\nres2: %i\noffset: %i\n", file_header.type, file_header.size, file_header.res1, file_header.res2, file_header.offset);
      fclose(fp);
  
      return 0;
  }
  </code></pre>