man, the help tonight ... you see that he''s outputing a fixed size, and reading back in a fixed size ... isn''t that a clue ...
here''s the deal ... you are using simple binary writing and reading functions, which work just like memcpy, but to a file ... right ...
BUT YOUR STRUCTURE HAS POINTERS IN IT ... and pointers cannot be stored out to a file and used again later ...they don''t mean anything later ... and the actual data pointed to won''t be there ...
think about it ... if you had a class like this:
struct BinarySafe {
int x;
int y;
char name[30];
};
then you can see that it works just fine .. cause it would be 38 bytes long, including all the data, and nothing but data ...
but if you had this:
struct UsesPointers {
int x;
int y;
char *name;
};
then you would be writing and reading 12 bytes ... the last 4 of which are just a pointer .. and NONE of which is a name.
this same thing would be true of any situation using pointers, and should be assumed to be true of any situation using classes ... (because you cannot copy classes into each other binary, you MUST use constructors) ....
the ANSWER is that you must go a level deeper to output each of your poitner or class objects ... for example, to read and write the UsesPointers struct above , you might do this:
void write_c_str(FILE *file, char *str) { short len = strlen(str); // get the string length fwrite(&len, sizeof(len), 1, file); // write the string length fwrite(str, len, 1, file); // write the string }void read_c_str(FILE *file, char **str) { short len; fread(&len, sizeof(len), 1, file); // read the string length delete [] *str; // release any old memory in str *str = new char[len]; // allocate enough space for new string fread(*str, len, 1, file); // output the string }void save(UsesPointers *up) { fwrite(up->x, sizeof(up->x), 1, file); fwrite(up->y, sizeof(up->y), 1, file); write_c_str(file, up->name); }void load(UsesPointers **up) { fread(up->x, sizeof(up->x), 1, file); fread(up->y, sizeof(up->y), 1, file); read_c_str(file, &(up->name)); }
Good Luck ... and these are just samples ... but I hope you see what I''m talking about now ...