- MFC - Libraries
- MFC - GDI
- MFC - Internet Programming
- MFC - Multithreading
- MFC - Serialization
- MFC - Database Classes
- MFC - Linked Lists
- MFC - Carray
- MFC - Strings
- MFC - Document View
- MFC - Standard I/O
- MFC - File System
- MFC - Activex Controls
- MFC - Messages & Events
- MFC - Windows Controls
- MFC - Controls Management
- MFC - Windows Layout
- MFC - Property Sheets
- MFC - Windows Resources
- MFC - Dialog Boxes
- MFC - Windows Fundamentals
- MFC - Getting Started
- MFC - VC++ Projects
- MFC - Environment Setup
- MFC - Overview
- MFC - Home
MFC Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MFC - Seriapzation
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.
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.
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.
Step 9 − Enter the info in all the fields and cpck Save and close this program.
Step 10 − It will save the data. Run the apppcation again and cpck open. It will load the Employee information.
Advertisements