How to read/write an object of MyClass from/to a file.
-
I want using VC++ to read/write an object of MyClass from/to a file. I've write CFile myFile; ... myFile.Write((char*)&myObject, sizeof(myObject)); So in my file there is a stranger string :( How can I do properly? I'm a beginning of programing. Please help me! Best regard!
-
I want using VC++ to read/write an object of MyClass from/to a file. I've write CFile myFile; ... myFile.Write((char*)&myObject, sizeof(myObject)); So in my file there is a stranger string :( How can I do properly? I'm a beginning of programing. Please help me! Best regard!
Hi, You cann't write directly class into File. You must use Serialization, and serialize each memeber from your class. See the sample below and look into MSDN for more Info. class CPerson : public CObject { public: DECLARE_SERIAL( CPerson ) // empty constructor is necessary CPerson(){}; CString m_name; WORD m_number; void Serialize( CArchive& archive ); // rest of class declaration }; void CPerson::Serialize( CArchive& archive ) { // call base class function first // base class is CObject in this case CObject::Serialize( archive ); // now do the stuff for our specific class if( archive.IsStoring() ) archive << m_name << m_number; else archive >> m_name >> m_number; } a
-
I want using VC++ to read/write an object of MyClass from/to a file. I've write CFile myFile; ... myFile.Write((char*)&myObject, sizeof(myObject)); So in my file there is a stranger string :( How can I do properly? I'm a beginning of programing. Please help me! Best regard!
-
I want using VC++ to read/write an object of MyClass from/to a file. I've write CFile myFile; ... myFile.Write((char*)&myObject, sizeof(myObject)); So in my file there is a stranger string :( How can I do properly? I'm a beginning of programing. Please help me! Best regard!
thanks all! :)
-
Hi, You cann't write directly class into File. You must use Serialization, and serialize each memeber from your class. See the sample below and look into MSDN for more Info. class CPerson : public CObject { public: DECLARE_SERIAL( CPerson ) // empty constructor is necessary CPerson(){}; CString m_name; WORD m_number; void Serialize( CArchive& archive ); // rest of class declaration }; void CPerson::Serialize( CArchive& archive ) { // call base class function first // base class is CObject in this case CObject::Serialize( archive ); // now do the stuff for our specific class if( archive.IsStoring() ) archive << m_name << m_number; else archive >> m_name >> m_number; } a
I've got the error: error LNK2001: unresolved external symbol "public: virtual struct CRuntimeClass * __thiscall CStudent::GetRuntimeClass(void)const " (?GetRuntimeClass@CStudent@@UBEPAUCRuntimeClass@@XZ) Please help!