Solved

About Serializing Complex Data

Posted on 2006-11-07
3
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 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