Object serialization problem: opening and reading a saved object.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Object serialization problem: opening and reading a saved object.

Hybrid View

  1. #1
    Join Date
    Feb 2011
    Posts
    39

    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?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    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.

  3. #3
    Join Date
    Feb 2011
    Posts
    39

    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

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    Re: Object serialization problem: opening and reading a saved object.

    I wouldn't try to create two CArchives on the same file.

  5. #5
    Join Date
    Feb 2011
    Posts
    39

    Re: Object serialization problem: opening and reading a saved object.

    Quote Originally Posted by GCDEF View Post
    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.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    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.

  7. #7
    Join Date
    Feb 2011
    Posts
    39

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center