Solved

About Serializing Complex Data

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

910 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

15 Experts available now in Live!

Get 1:1 Help Now