#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    1
    Rep Power
    0

    Exclamation Problem writing a struct....


    The following piece of code shows a strange error, which is almost certainly something I'm doing. The output that comes out in the file is wrong - it comes out as (Hex view):

    424d ffbf ffff ffff 4861 0140 8009 24

    (Note everything after ffff ffff is random non-initialised memory).

    The problem is that there's an extra 2 bytes being written in - "ffbf" . Can anyone see what the error is?

    Code:
    #include <iostream>
    #include <fstream>
    
    struct BITMAPFILEHEADER {    /* bmfh */
      char          bfType[2];
      unsigned int  bfSize;
      unsigned int  bfReserved1;
      unsigned int  bfOffBits;
    };
    
    std::ofstream fo(filename, std::ios::binary);
    if(fo)
    {
     BITMAPFILEHEADER bfh;
     bfh.bfType[0] = 'B';
     bfh.bfType[1] = 'M'; 
     bfh.bfSize = 0xFFFFFFFF;
      
     fo.write((char*)&bfh, sizeof(BITMAPFILEHEADER));
      
     fo.close();
    }
  2. #2
  3. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    Most likely that's padding to make sure that the unsigned int following the char[2] starts at a 4-byte boundary. Computers like it that way :)
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/

IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo