YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   Home Products | Purchase Support | Downloads  
View in English
View in Japanese
View in
참고
View in Français
View in Italiano
View in 中文(繁體)
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Overview
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
Purchase
ActiveX COM Products
Overview
Download
Purchase
Technical Support
  General Q & A
Discussion Board
Contact Us

Links

Get Ready to Unleash the Power of UCanCode .NET


UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"


VC++ Article: DECLARE_SERIAL and IMPLEMENT_SERIAL and serialize data from or to disk.

 
 

Introduction

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.
    Collapse Copy Code
    //CInfo.h
    
    class CInfo : public CObject
    {
    DECLARE_SERIAL(CInfo)
    
    public:
    
        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.
    Collapse Copy Code
    //CInfo.cpp
    
    #include "stdafx.h"
    #include "CInfo.h"
    
    IMPLEMENT_SERIAL(CInfo,CObject,1)
    
    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 :-
    Collapse Copy Code
    // 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, m_bCanSave is declared in the header file as BOOL m_bCanSave; and in the constructor it has been initialized as m_bCanSave = FALSE;
    Collapse Copy Code
    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;
        }
        else
        {
            int nTemp = _access(DATA_FILE, 6);  
            // Check Read Write Permissions
    
            if(nTemp==0)
                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);
                UpdateData(TRUE);
                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.
    Collapse Copy Code
    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);
                Info.Serialize(ar);
            }
    
        }
    
        m_nIndex = Info.m_nIndex;
        m_strName = Info.m_strName;
        UpdateData(FALSE);
    }

Conclusioning

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.

Contact UCanCode Software

To buy the source code or learn more about with:

 

 

Ask any questions by MSN: ucancode@hotmail.com Yahoo: ucan_code@yahoo.com


 

Copyright ?1998-2024 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.net