-
May 17th, 2011, 12:59 PM
#1
Object serialization problem: opening and reading a saved object.
Hello all,
I'm using serialization to save an object to a file. Here's the method:
Code:
//Saver
void Table :: Serialize(CArchive &cArch1)
{
CObject::Serialize(cArch1);
if(cArch1.IsStoring())
cArch1 << csName << nSerialNumber << bCreated;
else
cArch1 >> csName >> nSerialNumber >> bCreated;
cArch1.Close();
}
//Done
The object is saved with no problem into the required file. However, when I try to open it, i get a fatal error message. I began debugging it to trace it to the exact line, but the compiler compiles comfortably upto the line just before the cArch1.Close() line and exits the method. It then proceed to this snippet from a "Wincore.cpp":
Code:
CATCH_ALL(e)
{
lResult = AfxProcessWndProcException(e, &pThreadState->m_lastSentMsg);
TRACE(traceAppMsg, 0, "Warning: Uncaught exception in WindowProc (returning %ld).\n",
lResult);
DELETE_EXCEPTION(e);
}
END_CATCH_ALL
What is happening? Why is the saving happening but the loading not happening?
-
May 17th, 2011, 01:08 PM
#2
Re: Object serialization problem: opening and reading a saved object.
You might want to show the code where you open the file. You also may want to try to catch the exception yourself and see what the error actually is.
Also, never close the CArchive inside a serialize function. That's going to cause problems if you have more than one object, and that may well be your problem here.
-
May 17th, 2011, 01:17 PM
#3
Re: Object serialization problem: opening and reading a saved object.
Here's the code where i open the file. This method decides based on a parameter nOp whether to save or to load. 1 is to save and 0 is to load.
Code:
//Save or load the object!
void CMainWindow :: SaveOrOpenObject(int nOp)
{
cfSaveFile.Open(_T("Profile.txt"), CFile::modeCreate | CFile::modeReadWrite);
CArchive cArchSave(&cfSaveFile, CArchive::store), cArchOpen(&cfSaveFile, CArchive::load);
if(nOp == 1)
cTable.Serialize(cArchSave);
else if(nOp == 0)
cTable.Serialize(cArchOpen);
cArchSave.Close(); //incorporated changes. i'm closing the CArchive objects
cArchOpen.Close(); //outside the Serialize(CArchive &ca) method.
cfSaveFile.Close();
}
//Done
-
May 17th, 2011, 01:21 PM
#4
Re: Object serialization problem: opening and reading a saved object.
I wouldn't try to create two CArchives on the same file.
-
May 17th, 2011, 01:30 PM
#5
Re: Object serialization problem: opening and reading a saved object.
Originally Posted by GCDEF
I wouldn't try to create two CArchives on the same file.
I see. So how could I initiate the open and load routine? I'm trying to get CArchive::IsStoring() to be false and CArchive::IsLoading() to be true. There is no function to set this change.
-
May 17th, 2011, 01:41 PM
#6
Re: Object serialization problem: opening and reading a saved object.
Create the CArchive inside the scope of your if statement or create two functions, one for reading and one for writing.
I'm not sure that CFile::modeCreate is a good option here either for reading. It's going to delete your existing file every time you open it.
-
May 17th, 2011, 01:51 PM
#7
Re: Object serialization problem: opening and reading a saved object.
I fixed it. The CFile::modeCreate was causing the problem all along. Thanks very much, you've been very helpful!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|