Answered by:
Store and retrieve data from a text file in mfc

Question
-
I am using a text file to store and retrieve data for my application. I can able to create and write into the file but when i am trying to write the object of my file. I am getting the output strcore.cpp afxt in my text file.
Below is the code.
class store{
...........
............
};
store r;
CFile file;
file.Write((const void*)r,sizeof(r));
Thanks in advance
coder dinesh
Friday, January 20, 2012 5:48 AM
Answers
-
I am using a text file to store and retrieve data for my application. I can able to create and write into the file but when i am trying to write the object of my file. I am getting the output strcore.cpp afxt in my text file.
Below is the code.
class store{
...........
............
};
store r;
CFile file;
file.Write((const void*)r,sizeof(r));
Thanks in advance
coder dinesh
What you want is some form of object serialization. There's many libraries and approaches for that, and MFC has support. It would be something like this:
HTH.store.h: class store : public CObject { DECLARE_SERIAL(store); public: store(); virtual void Serialize(CArchive& ar); private: double data1; char data2; CString data3; }; store.cpp: IMPLEMENT_SERIAL(store, CObject, VERSIONABLE_SCHEMA|1) void store::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << data1 << data2 << data3; } else { ar >> data1 >> data2 >> data3; } } store::store() : data1(12345.678) , data2('C') , data3(_T("CoderDinesh")) { } storeload.cpp: #include "store.h" void Save(store& s, const CString& file) { CFile f(file, CFile::modeCreate); CArchive a(&f, CArchive::store); s.Serialize(a); } void Load(store& s, const CString& file) { CFile f(file, CFile::modeRead); CArchive a(&f, CArchive::load); s.Serialize(a); }
- Proposed as answer by Raman-SGGS Friday, January 20, 2012 10:00 AM
- Marked as answer by Jesse JiangModerator Thursday, January 26, 2012 6:10 AM
Friday, January 20, 2012 7:37 AM -
You are casting an object (r) to a void pointer, which makes no sense. You probably want to cast the address of the object to a void pointer:
(const void*)&r
- Marked as answer by Jesse JiangModerator Thursday, January 26, 2012 6:10 AM
Friday, January 20, 2012 2:38 PM
All replies
-
To read a text file :
if(file.Open(sFileName,CFile::modeRead)) { try { while(file.ReadString(sLine)); file.Close(); } catch(CException* pException) { pException->Delete(); } }
to write :
CStdioFile fileSrc,fileTmp; if(! fileSrc.Open(sFileName,CFile::modeReadWrite))return; try { CStdioFile fileTmp(sFileNameTemp,CFile::modeWrite | CFile::modeCreate | CFile::typeBinary); while(fileSrc.ReadString(sLine)) { fileTmp.WriteString(sLine); fileTmp.WriteString(_T("\n")); } fileSrc.Close(); fileTmp.Close(); CFile::Remove(sFileName); CFile::Rename(sFileNameTemp,sFileName); } catch(CException* pException) { pException->Delete(); }
Friday, January 20, 2012 6:16 AM -
Actually i don't want to store the string. I want to store it as an object of user-defined type, because i want to pick the certain data from the file.Friday, January 20, 2012 6:34 AM
-
I am using a text file to store and retrieve data for my application. I can able to create and write into the file but when i am trying to write the object of my file. I am getting the output strcore.cpp afxt in my text file.
Below is the code.
class store{
...........
............
};
store r;
CFile file;
file.Write((const void*)r,sizeof(r));
Thanks in advance
coder dinesh
What you want is some form of object serialization. There's many libraries and approaches for that, and MFC has support. It would be something like this:
HTH.store.h: class store : public CObject { DECLARE_SERIAL(store); public: store(); virtual void Serialize(CArchive& ar); private: double data1; char data2; CString data3; }; store.cpp: IMPLEMENT_SERIAL(store, CObject, VERSIONABLE_SCHEMA|1) void store::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << data1 << data2 << data3; } else { ar >> data1 >> data2 >> data3; } } store::store() : data1(12345.678) , data2('C') , data3(_T("CoderDinesh")) { } storeload.cpp: #include "store.h" void Save(store& s, const CString& file) { CFile f(file, CFile::modeCreate); CArchive a(&f, CArchive::store); s.Serialize(a); } void Load(store& s, const CString& file) { CFile f(file, CFile::modeRead); CArchive a(&f, CArchive::load); s.Serialize(a); }
- Proposed as answer by Raman-SGGS Friday, January 20, 2012 10:00 AM
- Marked as answer by Jesse JiangModerator Thursday, January 26, 2012 6:10 AM
Friday, January 20, 2012 7:37 AM -
You are casting an object (r) to a void pointer, which makes no sense. You probably want to cast the address of the object to a void pointer:
(const void*)&r
- Marked as answer by Jesse JiangModerator Thursday, January 26, 2012 6:10 AM
Friday, January 20, 2012 2:38 PM