EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

Serialization is not just storing or retrieving information to and from disk filesystem. It is generic to any file stream class. CArchive class is used to make it generic to any stream.

Serialization routine does not directly deal with CFile. It uses CArchive object instead of CFile. CArchive class decouples the file object from Serialize() routine.

Serialization routine

Serialize routine has this prototype.

void Serialize(CArchive& ar)

Let us look into a simple example. Here we declared a class called CStudent to store and retrieve fields of a student. It contains standard, enrollment number, name, marks and age. We are saving or restoring these members Serialize routine.

class CStudent : public CObject
public :
  WORD    m_wStd;
  WORD    m_wRoll;
10    CString m_sName;
11    double  m_dMarks;
12    WORD    m_wAge;
14  public :
15    void Serialize(CArchive& ar);
17    DECLARE_SERIAL(CStudent);
18  };
20  IMPLEMENT_SERIAL(CStudent, CObject, 0)
22  /* Main Serialize routine */
23  void CStudent::Serialize(CArchive& ar)
24  {
25    CObject::Serialize(ar);
27    if( ar.IsStoring() ) {
28      /* Serialize STD, ROLL, NAME, MARKs, AGE to file */
29      ar << m_wStd << m_wRoll << m_sName << m_dMarks << m_wAge;
30    } else {
31      /* Read STD, ROLL, NAME, MARKs, AGE from file */
32      ar >> m_wStd >> m_wRoll >> m_sName >> m_dMarks >> m_wAge;
33    }
35  }

CArchive and Serialization

Serialization routine uses CArchive object and CArchive is constructed from a generic file stream. This is to support any type of stream classes in Serialize routine and make it independent of file stream. Thus Serialize is not limited to disk/storage files. It can be socket file to Serialize in network stream or serial stream file transmitted in serial port.

/* Serialize routine for data saving */
void OnSave() 
  CXXXFile file;
  /* Open stream for writing */
  file.Open(< file >, < write mode >);
  /* pass file to CArchive constructor */
10    CArchive ar(&file, CArchive::store);
11    m_Student.Serialize(ar);
12    ar.Close();
13    file.Close();
14  }
16  /* Serialize routine for data retrieving */
17  void OnOpen() 
18  {
19    CXXXFile file;
20    /* Open stream for writing */
21    file.Open(< file >, < read mode >);
22    /* pass file to CArchive constructor */
23    CArchive ar(&file, CArchive::load);
24    m_Student.Serialize(ar);
25    ar.Close();
26    file.Close();
27    UpdateData(FALSE);
28  }

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Wed 30 Apr/2014 23:33:19(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.


Your message goes here:

My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.