// User account structure
struct ACCOUNT {
CString name; // name
char password[25]; // password
ONACC chars[10]; // index of characters owned
int totalchars;
bool master; // is the account holder a GameMaster
bool InUse;
CPerfTimer idle;
HANDLE Reserve;
ACCOUNT() {
Reserve = CreateEvent(NULL, FALSE, TRUE, NULL);
}
};
bool SAVEACC(ACCOUNT &newacc) {
CFile accfile;
CFileException fileException;
char accname[25];
sprintf(accname, "Database\\Accounts\\%s.acc", newacc.name);
if (!accfile.Open(accname, CFile::modeCreate | CFile::modeReadWrite, &fileException)) {
return false;
} else {
accfile.Write((char *)(&newacc), sizeof(newacc));
accfile.Close();
return true;
}
}
bool LOADACC(ACCOUNT &loadacc, char accname[25]) {
CFile accfile;
CFileException fileException;
if (!accfile.Open(accname, CFile::modeReadWrite, &fileException)) {
return false;
} else {
accfile.Read((char *)(&loadacc), sizeof(loadacc));
accfile.Close();
return true;
}
}
CString and CFile
I have the following basic code:
The problem is, that everything saves and loads perfectly fine except for the CString, which when loaded ends up being complete junk. How can I save it properly and then load it properly? Any help would be great.
The problem may be that "sizeof" returns the size of your struct (i.e the amount of memory needed to create your struct in memory), but not the total size of what your struct is pointing to.
In your case the Cstring dynamically allocates memory for the string. So you should handle it the same as having a pointer to a string or a pointer to a char array in your struct.
So to save your Cstring var you might need to use a function like strlen to get the total number of bytes you need to save.
I think you can cast a Cstring to a null terminated string, then it is easy to get the length and save it.
Alternatively CString may have a Length or Size property that you can use directly, but you will have to investigate.
In your case the Cstring dynamically allocates memory for the string. So you should handle it the same as having a pointer to a string or a pointer to a char array in your struct.
So to save your Cstring var you might need to use a function like strlen to get the total number of bytes you need to save.
I think you can cast a Cstring to a null terminated string, then it is easy to get the length and save it.
Alternatively CString may have a Length or Size property that you can use directly, but you will have to investigate.
Don''t do it that way. Any write(void/char/any_type*, size_t) function will break the encapsulation of your struct/class. It will store the binary image of your struct and nothing more, nothing less. It is not what you want. You should store each member separetly to the file, and for your CString you should store the its length, and its data.
Read this GameDev.net topic for more info: saving objects to files[^]
Update GameDev.net system time campaign:
Read this GameDev.net topic for more info: saving objects to files[^]
Update GameDev.net system time campaign:
''date ddmmHHMMYYYY''
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement