Click here to Skip to main content
13,291,254 members (61,029 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 28 Jun 2002

Serialization in non - Document/View arch. projects .

, 28 Jun 2002
Rate this:
Please Sign up or sign in to vote.
This explains how to serialize objects in non document/view architecture projects


This article will show you how you can serialize data to disk and retrieve it again in non doc/view architecture projects. Just follow the steps outlined.

Steps to follow

  • Create a normal project as you usually do.
  • Add the class which you would use to serialize the data. It might contain just normal variables or collection classes or any kind of data that you want to store.
    • Be sure to derive that class from CObject. Only then you can serialize the data.
    • Be sure to use the DECLARE_SERIAL macro inside.
  • //CInfo.h
    class CInfo : public CObject
        virtual void Serialize(CArchive& ar);
        int m_nIndex;
        CString m_strName;
  • Add the serialization function in this class's .cpp file. Be sure to use the IMPLEMENT_SERIAL macro as shown.
  • //CInfo.cpp
    #include "stdafx.h"
    #include "CInfo.h"
    void CInfo::Serialize(CArchive& ar)
        if (ar.IsLoading()) // If you are loading data from the disk
            ar >> m_strName;
            ar >> m_nIndex;
        else // If you are storing data to the disk
            ar << m_strName;
            ar << m_nIndex;
  • As this example is concerned with just 2 variables, assume that 2 edit controls have been created to accommodate the Index and the Name. Add 2 variables to those edit controls using the Class Wizard. Now the File to which the data has to be written or read from has to be defined. I do that simply by :-
  • // The Accounts.dat file is the 
    // file that we are going to use 
    // to save and read data
    #define DATA_FILE _T("Accounts.dat") 
  • Now, we write the code for storing the data to the disk. Here,
    is declared in the header file as BOOL m_bCanSave; and in the constructor it has been initialized as m_bCanSave = FALSE;
  • void SaveRecords()                  
    //  Code to serialize and save the data
        UINT nFlags = CFile::typeBinary | CFile::modeWrite;
        if (_access(DATA_FILE, 0))
            nFlags |= CFile::modeCreate;            
            // The file doesn't exist, so create it
            m_bCanSave = TRUE;
            int nTemp = _access(DATA_FILE, 6);  
            // Check Read Write Permissions
                m_bCanSave = TRUE;
        if (m_bCanSave)
            CFile file;
            CFileException fe;
            // The file exists with read & write permissions
            if (file.Open(DATA_FILE, nFlags, &fe))
                CArchive ar(&file, CArchive::store);
                Info.m_nIndex = m_nIndex;
                Info.m_strName = m_strName;
                Info.Serialize(ar); // Serialize the data
  • Now , we write the code for reading the data from the disk.
  • void LoadRecords()
        if (_access(DATA_FILE, 6)==0) // If File Exists
            CFile file;
            CFileException fe;
            if (file.Open(DATA_FILE,CFile::typeBinary | 
                CFile::modeRead, &fe))
                CArchive ar(&file, CArchive::load);
        m_nIndex = Info.m_nIndex;
        m_strName = Info.m_strName;


That's it. A Simple tutorial for serialization is complete. You have a program that can serialize data to the disk and retrieve it later. You didn't know that serialization was so easy. Did you? All Luck and have a nice time.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralWriting to Text file .Unable to write if the file is open Pin
Divya Lalwani8-Mar-09 22:02
memberDivya Lalwani8-Mar-09 22:02 
Questionserializing file through HTTP? Pin
xxhimanshu30-Oct-03 20:39
memberxxhimanshu30-Oct-03 20:39 
Generalhelp Pin
annim annuar6-Oct-03 17:27
memberannim annuar6-Oct-03 17:27 
GeneralNice! Pin
Ravi Bhavnani3-Jun-03 3:51
memberRavi Bhavnani3-Jun-03 3:51 
GeneralHelp Serializing files over HTTP?? Pin
xxhimanshu30-Oct-03 20:48
memberxxhimanshu30-Oct-03 20:48 
GeneralRe: Help Serializing files over HTTP?? Pin
Ravi Bhavnani31-Oct-03 3:53
memberRavi Bhavnani31-Oct-03 3:53 
GeneralRe: Help Serializing files over HTTP?? Pin
xxhimanshu31-Oct-03 21:28
memberxxhimanshu31-Oct-03 21:28 
GeneralRe: Help Serializing files over HTTP?? Pin
Ravi Bhavnani1-Nov-03 6:16
memberRavi Bhavnani1-Nov-03 6:16 
Generaldeleting record Pin
dudic20-Mar-03 10:10
memberdudic20-Mar-03 10:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 29 Jun 2002
Article Copyright 2002 by VGirish
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid