Reading from binary problem

Started by
2 comments, last by Metal Typhoon 18 years, 12 months ago
I have one program that it write a binary file.. for another program to read it and then used the data.. from within the program that writes the binary i test the structure that i'm writing , by opening the file reading the data and sending to a Out.txt file.. everything works fine...

// Write File
ofstream File (Filename,ios::binary);
File.write ((unsigned char *) &Window,sizeof (Window));
File.close ();
	
// Open file and write outputs
t_Window Test;
ifstream iFile (Filename,ios::binary);
ofstream oFile ("Out.txt",ios::out);

iFile.read ((unsigned char *) &Test,sizeof (Test));
iFile.close ();
oFile << Test.mTitle <<endl;
but from within the other program i try the SAME.. i copy and pasted the same code above starting from whre it says //Open file and write ... well.. the Out.txt file surprises me.. i get a bunch of ÌÌÌÌÌÌ .. i dont know why.. this is where my problem is.. the t_Window structure has mWidth mHeight as part of the data.. if i try to write mWidth or anyother data.. it goes fine... but exept the Title.. here is the structure.

typedef struct
{
  char *mTitle;
  int mXCoord;
  int mYCoord;
  int mWidth;
  int mHeight;
  int mBits;
  int mColors;
  Tstyle mstyle;
  TState mFullScreen;
  t_Viewport *mViewport;

} t_Window;

Metal Typhoon
Advertisement
No wonder: you're writing a pointer into your file!

The mTitle and mViewport members are pointer.
For example mTitle is just an int containing the adress of another memory location where your title is stored. You write this int in the file. Reading it again in the same program is no problem. The string will still be at that location.

Reading this pointer into another program will just crash/bug/print garbage, because there can be anything at this adress, but most probably not your title string ;-)

Serialization is a bit more complicated than that (specially for the viewport). Look it up in google.
If you want a simple (but probably not the best) solution to your title problem, use a char[] instead of a char* (not that you should be using std:string anyway...).

Regards,
jods
[edit]Doh! After two crashes/reboots when typing the first sentence of this post, there was still no response. But I came in second anyway. Darn you jods for being so helpful! [smile][/edit]

Your structure contains pointers. This means that you can't simply read and
write this structure byte-by-byte.

When you create the file, the contents of the structure get written to the file exactly as they are. Which means that the pointers get written to the file, not what the pointers actually point to. When you read the file back in, in the same program, then the pointer gets read in, and the pointer still points to memory that is valid. But when you read the file in another program, the pointer is pointing to garbage, thus you get garbage.

You'll have to manually write objects/arrays that are pointed to in the structure. For strings, you either need to write out the whole string, along with the terminating '\0', and then read it in one character at a time until you find the '\0', or write out the length of the string first, and then the string. Then when you read in the string, you can read in the length, and then you know exactly how many bytes to read in after that. For the viewport, well, that depends on the contents of the viewport structure.

The main point is, though, that you can't naively write complex structures to a file as you are doing. It'll take more code and such to make it work correctly.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
thank you very much :D going back to wrok at it now
Metal Typhoon

This topic is closed to new replies.

Advertisement