+ Reply to Thread
Results 1 to 2 of 2

Thread: Using CArchive class in MFC

  1. #1

    Using CArchive class in MFC

    Using CArchive class in MFC

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    CArchive is used to serialize and de-serialize data.

    Example:

    Code:
    class CStudent
    {
    private:
    	int		m_nRoleID ;
    	CString m_strName ;
    public:
    	CStudent()
    	{
    	}
    
    	CStudent(int nRoleID, CString strName)
    	{
    		m_nRoleID = nRoleID ;
    		m_strName = strName ;
    	}
    
    	void Archive(bool bIsStore, CString strFileName)
    	{
    		if (bIsStore)
    		{
    			CFile objFile(strFileName, CFile::modeCreate | CFile::modeWrite) ;
    			CArchive objArchive(&objFile, CArchive::store);
    			
    			objArchive << m_nRoleID ;
    			objArchive << m_strName ;
    		}
    		else
    		{
    			CFile objFile(strFileName, CFile::modeRead) ;
    			CArchive objArchive (&objFile, CArchive::load);
    			
    			objArchive >> m_nRoleID ;
    			objArchive >> m_strName ;
    		}
    	}
    } ;
    
    void UseSerializationStore()
    {
    	CStudent objStudent(1, "Hemen") ;
    	objStudent.Archive(true, "C:\\Student.txt") ;
    }
    
    void UseSerializationRestore()
    {
    	CStudent objStudent ;
    	objStudent.Archive(false, "C:\\Student.txt") ;
    }
    
    void UseSerialization()
    {
    	UseSerializationStore() ;
    	UseSerializationRestore() ;
    }

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts