0

Hi,

I really need some help. I need to write a structure to disk in c++ builder. I have been trying now for a couple of weeks and I am now about to give up. I have tried fread,fwrite etc. fstream as well as TFileStream with no luck.

I have now tried a simple program to write a structure containing :

struct s
{
   int a;
   String name;
};

<< moderator edit: added [code][/code] tags >>

I write this to disk and then try reading it back and I just get garbage if i read the two variables. If i close the program, reopen then try load straight away I can get the two variables back ok but then get an access violation. This is using fstream. fread and TFileStream just cause garbage and access violoations all the time.

I am using c++ bUilder 6.

Any ideas how I can save this structure to disk? Can someone give me some source code?

regards

Nathan

3
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by Narue
0

If you try to write the structure object to a file, you'll basically be doing a byte-by-byte copy to the file. That's bad because one of your members appears to be a non-POD type (String). A byte-by-byte copy on a non-POD type will likely not work, and because it's undefined behavior, could easily break.

Your only solution at this point is to serialize the object in whatever arbitrary manner you choose or write each member to file separately.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.