Solved

About Serializing Complex Data

Posted on 2006-11-07
3
227 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 163
sameEnds challenge 3 180
Use of condition with 'serial' in ansible 2 71
Thin secure Windows 10 5 102
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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