mr_mooz 0 Newbie Poster

Im trying to write a struct for a binary header of a sun raster file to a file on disk.

i have a struct for the header data:

struct rasterfile {
  int  ras_magic;
  int  ras_width;
  int  ras_height;
  int  ras_depth;
  int  ras_length;
  int  ras_type;
  int  ras_maptype;
  int  ras_maplength;
  };



which i then fill with the data for my raster file:

rasterfile fileheader;


void setHeader()
{ // Fills the raster header  with default values...

fileheader.ras_magic = 0x59a66a95;
fileheader.ras_width = 768;
fileheader.ras_height = 576;
fileheader.ras_depth = 24;
fileheader.ras_length = 0;//1327104;
fileheader.ras_type = 0; // RGB instead of BGR???
fileheader.ras_maptype = 0;
fileheader.ras_maplength = 0;

}

I then write to the file using this:

void writetofile()
{ //ouputs the header and the bitmap array to file

    ofstream myFile ("purple.ras", ios::out | ios::binary);
    myFile.write ((char*)&fileheader.ras_magic, 32);
    myFile.write ((char*)&fileheader.ras_width, 32);
    myFile.write ((char*)&fileheader.ras_height, 32);
    myFile.write ((char*)&fileheader.ras_length, 32);
    myFile.write ((char*)&fileheader.ras_type, 32);
    myFile.write ((char*)&fileheader.ras_maptype, 32);
    myFile.write ((char*)&fileheader.ras_maplength, 32);
    myFile.write ((char*)&bitmap, sizeof (bitmap));
    myFile.close();
    }

But when i open this in a graphics veiwer I get told that the header is invalid.

Any ideas on a better way to output a struct to a binary file???? Anyone done any raster stuff before??


Thanks!


Josh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.