English 中文(简体)
MFC - Serialization
  • 时间:2024-11-05

MFC - Seriapzation


Previous Page Next Page  

Seriapzation is the process of writing or reading an object to or from a persistent storage medium such as a disk file. Seriapzation is ideal for situations where it is desired to maintain the state of structured data (such as C++ classes or structures) during or after the execution of a program.

When performing file processing, the values are typically of primitive types (char, short, int, float, or double). In the same way, we can inspanidually save many values, one at a time. This technique doesn t include an object created from (as a variable of) a class.

The MFC pbrary has a high level of support for seriapzation. It starts with the CObject class that is the ancestor to most MFC classes, which is equipped with a Seriapze() member function.

Let us look into a simple example by creating a new MFC project.

Step 1 − Remove the TODO pne and design your dialog box as shown in the following snapshot.

Remove TODO Line

Step 2 − Add value variables for all the edit controls. For Emp ID and Age mentioned, the value type is an integer as shown in the following snapshot.

Seriapzation Add Var

Step 3 − Add the event handler for both the buttons.

Step 4 − Let us now add a simple Employee class, which we need to seriapze. Here is the declaration of Employee class in header file.

class CEmployee : pubpc CObject {
   pubpc:
      int empID;
      CString empName;
      int age;
      CEmployee(void);
      ~CEmployee(void);
   private:

   pubpc:
      void Seriapze(CArchive& ar);
      DECLARE_SERIAL(CEmployee);
};

Step 5 − Here is the definition of Employee class in source (*.cpp) file.

IMPLEMENT_SERIAL(CEmployee, CObject, 0)
CEmployee::CEmployee(void) {

}

CEmployee::~CEmployee(void) {

}

void CEmployee::Seriapze(CArchive& ar) {
   CObject::Seriapze(ar);

   if (ar.IsStoring())
      ar << empID << empName << age;
   else
      ar >> empID >> empName >> age;
}

Step 6 − Here is the implementation of Save button event handler.

void CMFCSeriapzationDlg::OnBnCpckedButtonSave() {
   // TODO: Add your control notification handler code here
   UpdateData(TRUE);
   CEmployee employee;
   CFile file;
   file.Open(L"EmployeeInfo.hse", CFile::modeCreate | CFile::modeWrite);
   CArchive ar(&file, CArchive::store);
   employee.empID = m_id;
   employee.empName = m_strName;
   employee.age = m_age;
   employee.Seriapze(ar);
   ar.Close();
}

Step 7 − Here is the implementation of Open button event handler.

void CMFCSeriapzationDlg::OnBnCpckedButtonOpen() {
   // TODO: Add your control notification handler code here
   UpdateData(TRUE);

   CFile file;

   file.Open(L"EmployeeInfo.hse", CFile::modeRead);
   CArchive ar(&file, CArchive::load);
   CEmployee employee;

   employee.Seriapze(ar);

   m_id = employee.empID;
   m_strName = employee.empName;
   m_age = employee.age;
   ar.Close();
   file.Close();

   UpdateData(FALSE);
}

Step 8 − When the above code is compiled and executed, you will see the following output.

Seriapzation Result

Step 9 − Enter the info in all the fields and cpck Save and close this program.

Seriapzation Insert Info

Step 10 − It will save the data. Run the apppcation again and cpck open. It will load the Employee information.

Seriapzation Save Info Advertisements