Solved

About Serializing Complex Data

Posted on 2006-11-07
3
198 Views
Last Modified: 2013-11-20
Hi all,
        I want to serialize complex data and save it to a CFile object. I have found a number of examples showing the serialization and storing of the data to the file. Please send me some links/examples showing the serialization of a CObject derived class containing about 4/5 arrays of structures holding various members of CString, int and float datatypes. How do I serialize such a class? And how do I save and retrieve the contents to/from the CFile object I have created?
Thankyou.
0
Comment
Question by:bhushanvi
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 125 total points
ID: 17896564
To save an array (or other container) you first save how many items there are in the container.

eg

ar << MyArray.GetSize()
for(int i = 0; i < MyArray.GetSize(); i++)
  ar<< MyArray[i].Contents();   // OR have a function to save each item - MyArray[i].Save(ar);


then to load you read how many items there are and read the individual items in a loop - the reverse of saving
0
 

Author Comment

by:bhushanvi
ID: 17900316
Hi,
    Your answer was quite a help! But my data array is not a member of CArray or any other array having homogeneous data. So, can you go ahead and tell me, how can I serialize the data and save it to a txt(?) file or whatever? Here is the prototype of what I am trying to achieve:

class MyObject : public CObject
{
    struct ONE
    {
       CString m1;
       int m2;
       float m3;
    }
    ONE First[20];
    int Num_First;   // holds the number of entries for struct ONE

    struct TWO
    {
        members......;
     }
     TWO Second[10];
     int Num_Second;   //holds the number of entries for struct TWO
     
     struct THREE
     {
         members.......;
     }
      THREE Third[15];
     int Num_Third;   // holds the number of entries for struct THREE

// Functions...

     f(1);
     f(2);
     f(3);
     
     Serialize(ar);      // Serialize function for custom class derived from CObject taking CArchive data as it's parameter.

}
// end of class declaration.

        The above details match to my requirements closely. Is there any way that this could be done?

Or do I have to declare each of those structs as classes derived from CObject and then serialize the data and then save it.

Or alternatively declare a CStringArray for each of the struct members separately and then get the values from the struct with a dot operator and then serialize/deserialize those arrays?

Please let me know in short the steps for how should I go abt it....
THANKYOU.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17900594
In the serialise just write/read the individual variables, one after the other
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now