how to Read/Write to a file a structure wich contains Cstring ?
-
Hello, I have to Write/read a file with a structure wich contains CString data, if drop out the CString data from the structure it works but if I left the CString data the program crashes when attemps to read. Here the structure
struct MyStructure
{
CString label//For others data types char, BYTE, etc it works but for CString it doesn't
unsignet char data1
BYTE data2
}the writing
struct MyStructure Estructura; CFile FileConfig;
FileConfig.Open("ROMconfig.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
FileConfig.Seek(0,CFile::begin);
FileConfig.Write(&Estructura,sizeof(Estructura));
FileConfig.Close();the reading
struct MyStructure Estructura; CFile FileConfig ;
FileConfig.Open("ROMconfig.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeRead);
FileConfig.Seek(0,CFile::begin);
FileConfig.Read(&Estructura,sizeof(Estructura));
FileConfig.Close();What I'm doing wrong, may be is something in relation with CString size, but I'm not sure. And the file has to be a .dat file because is modification to an older application.
-
Hello, I have to Write/read a file with a structure wich contains CString data, if drop out the CString data from the structure it works but if I left the CString data the program crashes when attemps to read. Here the structure
struct MyStructure
{
CString label//For others data types char, BYTE, etc it works but for CString it doesn't
unsignet char data1
BYTE data2
}the writing
struct MyStructure Estructura; CFile FileConfig;
FileConfig.Open("ROMconfig.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
FileConfig.Seek(0,CFile::begin);
FileConfig.Write(&Estructura,sizeof(Estructura));
FileConfig.Close();the reading
struct MyStructure Estructura; CFile FileConfig ;
FileConfig.Open("ROMconfig.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeRead);
FileConfig.Seek(0,CFile::begin);
FileConfig.Read(&Estructura,sizeof(Estructura));
FileConfig.Close();What I'm doing wrong, may be is something in relation with CString size, but I'm not sure. And the file has to be a .dat file because is modification to an older application.
You can't save and read your data this way. Internally, a CString object stores its data in a pointer, so you will save only the address of the pointer and not its content. I suggest you read this article[^] which is about serialization in MFC. You'll get some ideas about how to serialize your data properly.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
You can't save and read your data this way. Internally, a CString object stores its data in a pointer, so you will save only the address of the pointer and not its content. I suggest you read this article[^] which is about serialization in MFC. You'll get some ideas about how to serialize your data properly.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++Ok, thanks , i will check it, very usefull. But for the other data types is it ok to save data i as described before? , I mean pherhaps is not the elegant way but it's working (not with CString), as I have to finish this work for tomorrow I was hoping there was a easier way, I only have to store some few words.
-
Ok, thanks , i will check it, very usefull. But for the other data types is it ok to save data i as described before? , I mean pherhaps is not the elegant way but it's working (not with CString), as I have to finish this work for tomorrow I was hoping there was a easier way, I only have to store some few words.
It would be easier to provide a function to save and a function to load the structure (functions in the structure itself). These functions will save and load all members of the structure. For the CString type, you first have to save the lenght of the string then the string itself (when loading, you read the lenght and then read that many characters from the file).
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Hello, I have to Write/read a file with a structure wich contains CString data, if drop out the CString data from the structure it works but if I left the CString data the program crashes when attemps to read. Here the structure
struct MyStructure
{
CString label//For others data types char, BYTE, etc it works but for CString it doesn't
unsignet char data1
BYTE data2
}the writing
struct MyStructure Estructura; CFile FileConfig;
FileConfig.Open("ROMconfig.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
FileConfig.Seek(0,CFile::begin);
FileConfig.Write(&Estructura,sizeof(Estructura));
FileConfig.Close();the reading
struct MyStructure Estructura; CFile FileConfig ;
FileConfig.Open("ROMconfig.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeRead);
FileConfig.Seek(0,CFile::begin);
FileConfig.Read(&Estructura,sizeof(Estructura));
FileConfig.Close();What I'm doing wrong, may be is something in relation with CString size, but I'm not sure. And the file has to be a .dat file because is modification to an older application.