Click here to Skip to main content
Click here to Skip to main content

A serialization primer - Part 1

By , 25 Nov 2002
 

This article is the first of a 3 part tutorial on serialization.

  • Part 1 introduces the basics of serialization.
  • Part 2 explains how to gracefully handle reading invalid data stores and support versioning.
  • Part 3 describes how to serialize complex objects.

Serialization is the process of writing or reading an object to or from a persistent storage medium, such as a disk file. Serializing an object requires 3 ingredients:

  • A CFile object representing the datafile
  • A CArchive object that provides the serialization context
  • The object being serialized
Serialization data flow

Step 1 - Open the datafile

To serialize an object to the file "foo.dat", open the file with the appropriate access mode. In this example, the file is opened for exclusive read/write access.
  // Open file "foo.dat"
  CFile* pFile = new CFile();
  ASSERT (pFile != NULL);
  if (!pFile->Open ("foo.dat", CFile::modeReadWrite | CFile::shareExclusive)) {
      // Handle error
      return;
  }

Step 2 - Hook up the archive

Next, a CArchive object is hooked up to the file. The archive provides an efficient conduit to persistent storage. Instead of directly reading and writing the file, you serialize data to and from the archive. The archive needs to know if you're going to be using it to read or write data. In this example, we'll assume we're writing data.
  // Create archive ...
  bool bReading = false;  // ... for writing
  CArchive* pArchive = NULL;
  try
  {
    pFile->SeekToBegin();
    UINT uMode = (bReading ? CArchive::load : CArchive::store);
    pArchive = new CArchive (pFile, uMode);
    ASSERT (pArchive != NULL);
  }
  catch (CException* pException)
  {
    // Handle error
    return;
  }

Step 3 - Serialize the object

Finally, we serialize the object by calling its serialize() method. serialize() is just a method we made up. It has nothing to with MFC's CObject::Serialize(). Also, you don't have to derive your object from CObject. Our serialize() method takes a pointer to a CArchive and returns an integer status.
  int CFoo::serialize
    (CArchive* pArchive)
  {
    int nStatus = SUCCESS;

    // Serialize the object ...
    ...
    
    return (nStatus);
  }
We'll get to the actual serialization process in a minute. Meanwhile, let's recognize a couple of important points:
  • The same method CFoo::serialize() is used to read/write the object from/to persistent storage.
  • CFoo::serialize() doesn't know anything about the datafile bring accessed.
Assume CFoo represents an employee record that contains a couple of data members.
  class CFoo
  {
    // Construction/destruction
    public:
      CFoo::CFoo();
      virtual CFoo::~CFoo();

    // Methods
    public:
      int serialize (CArchive* pArchive);

    // Data members
    public:
      CString  m_strName;  // employee name
      int      m_nId;      // employee id
  };
We use CArchive's streaming operators << and >> to read/write the data members from/to the archive. CArchive knows how to serialize simple data types like int, float, DWORD, and objects like CString. The archive also knows whether it's in read or write mode. You can query its mode by calling CArchive::IsStoring(). CFoo's serialization method can then be written as:
  int CFoo::serialize
    (CArchive* pArchive)
  {
    int nStatus = SUCCESS;

    // Serialize the object ...
    ASSERT (pArchive != NULL);
    try
    {
      if (pArchive->IsStoring()) {
         // Write employee name and id
         (*pArchive) << m_strName;
         (*pArchive) << m_nId;
      }
      else {
         // Read employee name and id
         (*pArchive) >> m_strName;
         (*pArchive) >> m_nId;
      }
    }
    catch (CException* pException)
    {
      nStatus = ERROR;
    }
    return (nStatus);
  }

Step 4 - Clean up

When you've finished serializing, you should clean up by closing the archive and datafile.
  pArchive->Close();
  delete pArchive;
  pFile->Close();
  delete pFile();

Conclusion

Well, there you have it - serialization in a (very small) nutshell. In Part 2, we'll see how to gracefully handle reading invalid data stores and support different versions of our object. In Part 3, we'll see how to serialize complex objects.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ravi Bhavnani
Technical Lead
Canada Canada
Ravi Bhavnani is an ardent fan of Microsoft technologies who loves building Windows apps, especially PIMs, system utilities, and things that go bump on the Internet. During his career, Ravi has developed expert systems, desktop imaging apps, marketing automation software, EDA tools, a platform to help people find, analyze and understand information, trading software for institutional investors and advanced data visualization solutions. He currently works for a company that provides enterprise workforce management solutions to large clients.
 
His interests include the .NET framework, reasoning systems, financial analysis and algorithmic trading, NLP, CHI and UI design. Ravi holds a BS in Physics and Math and an MS in Computer Science and was a Microsoft MVP (C++ and C# in 2006 and 2007). He is also the co-inventor of 2 patents on software security and generating data visualization dashboards. His claim to fame is that he crafted CodeProject's "joke" forum post icon.
 
Ravi's biggest fear is that one day he might actually get a life, although the chances of that happening seem extremely remote.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4membersagar131015-Aug-12 20:56 
Good article.
Generalthank you !membermayjune5624-Jun-12 18:46 
in a short,its very good
GeneralRe: thank you !memberRavi Bhavnani25-Jun-12 1:16 
Thank you for your comments!
 
/ravi
My new year resolution: 2048 x 1536
Home | Articles | My .NET bits | Freeware
ravib(at)ravib(dot)com

GeneralThe links in the conclusion to the other parts are deadmemberHarrison H7-Oct-10 10:23 
Missing the "x" after .asp
GeneralRe: The links in the conclusion to the other parts are deadmemberRavi Bhavnani7-Oct-10 10:33 
Yes, unfortunately this is a site-wide problem. Frown | :(
 
/ravi
My new year resolution: 2048 x 1536
Home | Articles | My .NET bits | Freeware
 
ravib(at)ravib(dot)com

GeneralRe: The links in the conclusion to the other parts are deadmemberHarrison H7-Oct-10 10:38 
Boooo. But quick reply! Smile | :)
QuestionChanging the format of the data in the filememberAORD29-Sep-07 12:26 
What is the best way of making the data in the file unreadable?
 
Currently I can see all the stings and integers when I open the file in notepad.
Can the data be saved so the file is not readable by somebody opening with notepad etc.
 
Cheers,
AORD
 
___________________________
 
Here come the machines!

AnswerRe: Changing the format of the data in the filememberRavi Bhavnani9-Oct-07 17:51 
[ Sorry for the delay in replying. For some reason, I no longer receive CP emails when someone posts a note in my article forums. ]
 
A simple way to deter casual viewers is to invert the bits of the characters in a string before/after serialization. For example:
void InvertString
  (CString& strString)  // string to be inverted
{
  for (int nIndex=0; (nIndex < strString.GetLength()); nIndex++) {
      char ch = strString.GetAt (nIndex);
      ch = ~ch;
      strString.SetAt (nIndex, ch);
  }
}
Hope this helps!
 
/ravi
 
This is your brain on Celcius
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com

GeneralAbout the contents of the filemembercristitomi12-Feb-07 22:24 
Hi!
I am creating an object of type CFoo with some values for the data types, for example
m_strName = "someName"
m_nID = 98
and I am using the CArchive in store mode.
But when I look at the file it shows me something like:
 
someNameb
 
instead of the right data.
Does anyone know why this happens ?
Thanks.
 
cristitomi
AnswerRe: About the contents of the filememberRavi Bhavnani13-Feb-07 1:34 
cristitomi wrote:
Does anyone know why this happens ?

The file contains a binary representation of the data. If you'd like to see "98" in the file, you'd need to store the string version of m_nId.
 
Also, data stored for a CString will be preceded by its length (stored in binary form), which is required for the CArchive to know when to stop reading the CString.
 
/ravi
 
This is your brain on Celcius
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com

GeneralRe: About the contents of the filemembercristitomi13-Feb-07 2:22 
Hi!
Thanks for your helpful comments.
I can tell you this is a great article, very helpful to me indeed.
Thanks again.
GeneralRe: About the contents of the filememberRavi Bhavnani13-Feb-07 3:25 
Glad I could be of help, Cristi!
 
/ravi
 
This is your brain on Celcius
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com

Generaltrouble in reading in serialized list structuressussitsh1115-Dec-04 12:01 
I am trying to read in a list structure which is serialized as
 
m_DataPointerList.Serialize( ar );
 
where m_DataPointerList is defined as :
 
CTypedPtrList m_DataPointerList;
 
and class CDataPointer is declared as:
 
class CDataPointer : public CObject
{
public:
CDataPointer();
 
protected:
DECLARE_SERIAL(CDataPointer)
 
protected:
 
public:
unsigned long Time; unsigned char Channel;
unsigned char Rate;
 
public:
virtual void Serialize(CArchive& ar);
};
 
and defined as:
 
IMPLEMENT_SERIAL(CDataPointer, CObject, 1)
CDataPointer::CDataPointer()
{
 
}

void CDataPointer::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << Time;
ar << Channel;
ar << Rate;
}
else
{
ar >> Time;
ar >> Channel;
ar >> Rate;
}
}
 
I am having some difficulties( i donno why?) in reading it in using the same serializing command ie:
 
m_DataPointerList.Serialize( ar );
 
is there any thing i am overlooking while i am doing this??
 
on the other hand,
I try to read this using a 'for' loop
which iterates for the number of occurances of CDataPointer. but i have to add an offset at the end of every iteration if I were to read the next iteration correctly.
something like
 
nDataCounter=ar.ReadCount();
for(i=0;i>Time[i];
ar>>Channel[i];
ar>>Rate[i];
 
ar.Flush();
pFile->Seek(offSET, CFile::current);
}
 
i dont understand why i have to provide this offset.and i have trouble choosing the correct offSET as it varies for different lists. again is there something i am doing wrong ?
 
Can please you give me some insight into these two issues??
 
Thanks a lot
Hemanth
GeneralRe: trouble in reading in serialized list structures(typo corrected)memberitsh1116-Dec-04 4:11 
m_DataPointerList has to be defined as
 
CTypedPtrList < CObList,CDataPointer* > m_DataPointerList;
 
and not as wrongly typed by me
GeneralReading a file using serializationsusshemanth_phk9-Sep-04 11:55 
I am trying to read in a file, which was written using serialization (by someone else). The size of the variables present in the file from which I need to read is way greater than the stack size. So this is giving me problems like stack overflow. Is there any way to solve such problems? Please give me your suggestions .Thanks a lot
GeneralRe: Reading a file using serializationmemberRavi Bhavnani9-Sep-04 12:28 
See my reply in Part 3. No need to cross-post. Smile | :)
 
/ravi
 
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com

GeneralRe: Reading a file using serializationmemberThomas Mielke11-Oct-07 14:58 
Have just ran into it as well. Large data amounts are usually stored in pointer lists, but these are being processed recursively by CArchive resulting in extreme stack consumtion. Two workarounds that are not fully satisfying: disintegrate the list (by zeroing the 'next' pointer) and store each element with a distinct '<<' operation, or increase the default maximum stack size of 1 MB by using a linker option (for example /STACK:0xa00000 for 10 MB).
GeneralCArchive and .NETsussDaniel S. Horwitz12-Jul-04 6:37 
Dear Folks,
 
Is there a class in .NET that can read documents written using
the MFC CArchive class?
The problem is that I am porting an existing MFC C++
application to .NET. It uses CArchive to persist and
restore its documents. I need the ported version of
the code to be able to read the existing documents that
were persisted by CArchive.
 
Thanks for your help,
Daniel S. Horwitz

GeneralIn memory serializationmemberno_reg_name7-May-04 2:59 
Can you do this? Serialize an object and send it accross the wire, without actually storing it to the disk?
GeneralRe: In memory serializationmemberRavi Bhavnani7-May-04 6:02 
I believe this can be done by using CSocketFile instead of CFile.
 
/ravi
 
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com

GeneralSerialization using Dialog applicationmemberHybridLlama12-Jan-04 16:57 
How do i serialize data collected from a dialog using one button to save and one button to load.
GeneralRe: Serialization using Dialog applicationmemberRavi Bhavnani16-Jan-04 6:37 
Store the data in an object, then serialize the object.
 
/ravi
 
My new year resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com

GeneralRe: Serialization using Dialog applicationmemberHybridLlama16-Jan-04 9:56 
Thanks,
I got the serialization to work but I am having a problem loading the data. It is giving me an error something to the effect that an attempt was made to acessan unamed file past its end. How can I fix this?
GeneralRe: Serialization using Dialog applicationmemberRavi Bhavnani17-Jan-04 3:32 
It's hard to say without looking at the code. Perhaps you could post a snippet? It's possible that you may not have opened the file or hooked up the archive correctly.
 
/ravi
 
My new year resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com

GeneralRe: Serialization using Dialog applicationmemberHybridLlama17-Jan-04 8:28 
Here is a small segment of the storeing code and the loading code:
Storing:
 
CFile file;
 
file.Open(fileName, CFile::modeCreate|CFile::modeWrite);

CArchive ar(&file, CArchive::store);
 
Serialize(ar);
 
ar.Close();
file.Close();
 
Loading:
 
CFile file;
 
file.Open(filename,CFile::modeRead);
 
CArchive ar(&file, CArchive::load);
 
Serialize(ar);
 

The program would load the information just fine until I added 5 strings to be stored, after that it went down hill. I hope the above is what you wanted.

GeneralDitching.NET serialization in favor of MFCmemberMtnBiknGuy28-Aug-03 16:49 
Can anyone point me in the right direction for getting started using MFC serialization in place of .NET serialization?
 
We need to save large amounts (up to gigabytes) of complex objects and data to disk files. Most of the code is managed C#, but .NET serialization is way too slow. The processing time increases from 1 hr to over 15 hrs when we use .NET binary serialization (instead of just writing the data to ASCII files -- and the ASCII code even uses some reflection and other slow stuff).
 
Our desire is to keep most of our managed C# code, but substitute MFC serialization for .NET serialization in whatever way is required.
 
Any pointers or tips are apprecited.
GeneralRe: Ditching.NET serialization in favor of MFCmemberRavi Bhavnani29-Aug-03 4:33 
MtnBiknGuy wrote:
Any pointers or tips are apprecited
 
Aren't the articles sufficient? OMG | :OMG:
 
/ravi
 
Let's put "civil" back in "civilization"
Home | Articles | Freeware | Music
ravib@ravib.com

AnswerRe: Ditching.NET serialization in favor of MFCmemberGordon Brandly31-Aug-05 6:48 
Though I've been thinking of switching to .NET for our new data logging programs, this was something I've been worried about and a major reason I haven't switched yet. I'm glad to see that my fears are reasonable for once. Big Grin | :-D
 
Still, I do like the productivity of .NET, and I think my switchover is coming soon. When I do switch, I'll be using custom binary file formats like I do now. It's like working in assembly language: more work than using the built-in serialization libraries, but you can (potentially) get much faster performance.
 
On the other hand, I do less and less assembly language programming as time goes on, and I suspect that the .NET serialization support will get better in future versions.
GeneralProblem reading filememberCount Zer021-Jul-03 9:19 
I am trying to pull a bunch of CStrings out of a text file but I am not using an Object array
this is how I am do it
 
do
{
if( ar.IsLoading() )
{
ar >> temp;
}
 
if(temp.GetLength() > 1)
{
allnames[x] = temp;
x++;
}
 

}while(!temp.IsEmpty());
 
the problem is with ending the whole process I read all the CStrings but then it blows up when I read the next CString that is not there. How do I detect the end of file so that this will not happen.
 
thanks
GeneralRe: Problem reading filesussAnonymous16-Jan-04 5:43 
When writing the strings, store the number of strings written first.
 
James
Questionlibrary for XML serialization in C++ ?sussAnonymous17-Mar-03 1:15 
Hello,
 
Thanks to this article, I learnt a lot about serialization.
 
My goal is to serialize in a readable format. XML would be ok.
Moreover, I don't want to write everything from scratch, I prefer to use existing code.
 
Do you know any good (commercial) library to implement XML serialization in C++ ?
(I don't want to use managed C++).
 
Regards,
francois.

AnswerRe: library for XML serialization in C++ ?memberRavi Bhavnani17-Mar-03 1:30 
Hi Francois,
 
Sorry, I'm not familiar with commercial XML libs. Perhaps you could just use MSXML? Also see this article[^].
 
/ravi
 
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
AnswerRe: library for XML serialization in C++ ?memberRodrigo Pinho Pereira de Souza16-Aug-05 3:22 
Hi,
 
Boost has a serialization lib that supports XML serialization, and is portable.
 
www.boost.org
 
Bye
 
Rodrigo Pinho Pereira de Souza
GeneralNewbie Question using CStdioFilememberGregggggg23-Jan-03 16:55 
I have written a little project that takes strings in from text edit in a dialog box and writes them to file. I have been using WriteString with success. Below is an example of the snipped code showing one data member.
 
My question, I can't write anything but strings with this, apparently, is there a method similar to this to write ints? My choices to write to the file seem to be Write, WriteString and WriteHuge. I have had no success with Write and ints. WriteHuge seems to be for large strings.
 

CStdioFile file1;
file1.Open ( "Names.Txt", CFile::end );

file1.SeekToEnd();
 
m_Lname.GetWindowText (cs_Lname);

file1.WriteString (cs_Lname);

I could scrap what I have done and go with serialization as described in the article if I have to.
 
Thanks,
 
Greg
GeneralRe: Newbie Question using CStdioFilememberRavi Bhavnani4-Mar-03 4:44 
[Sorry for the delayed response]
 
You could simply format your int as a string and continue to use WriteString() to write the data. Serialization is more applicable when you want to be able to read/write objects with versioning support, and have the need for rudimentary file format validation.
 
/ravi
 
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
GeneralForget MFC CArchive/CFile...memberTBiker13-Jan-03 18:38 
This is not my idea of a clean implementation of serialization. You are assuming the use of pre-existing classes called CFile and CArchive. You are also assuming that flattening a data structure within a class only serves to save/recall from persistent storage. What if I want to send my class data across a TCP/IP socket or across other serial-based streams like Firewire or RS-232.
 
I suggest instead to introduce a base abstract class called CSerialize which defines two abstract functions:
 
class CSerialize
{
virtual BOOL read(unsigned char *buffer, int len) = 0;
virtual BOOL write(unsigned char *buffer, int len) = 0;
}
 
Then derive various implementations for each stream type (File, socket, etc.), for example:
 
class CFileStream : public CSerialize
{
CFileStream(LPSTR szFileName, BOOL bRead = TRUE)
{
_fp = fopen(szFileName, bRead ? _T("rb") : _T("wb"));
}
virtual ~CFileStream()
{
if (_fp) fclose(_fp);
_fp = NULL;
}
 
virtual BOOL read(unsigned char *buffer, int len)
{
if (!_fp) return FALSE;
return fread(_fp, len, 1, buffer) != 1 ? FALSE : TRUE;
}
 
virtual BOOL write(unsigned char *buffer, int len)
{
if (!_fp) return TRUE;
return fwrite(_fp, len, 1, buffer) != 1 ? FALSE : TRUE;
}
 
private:
FILE *_fp;
}
 
Then for each object you want to serialize, support << and >>
overloads, for example:
 
class CMyObject
{
CMyObject() { a = 5; b = 3.14; }
virtual ~CMyObject() {}
 
// data part
int a;
float b;
// etc.
 
// overloaded stream operators
operator << (CSerialize& s)
{
s << a;
s << b;
}
operator >> (CSerialize& s)
{
s >> a;
s >> b;
}
}
 
In the application , it all comes together as follows:
 
CFileStream fs1(_T("c:\\myobject.dat"), FALSE);
CMyObject m1;
 
// store object to file
fs1 << m1;
 
CFileStream fs2(_T("c:\\myobject.dat"), TRUE);
CMyObject m2;
 
// recall object from file
fs2 >> m2;
 
That's it. I just typed this design up so no guarantees I haven't made a mistake here but you get the general concept. Don't rely on MFC to do your design!
 



GeneralRe: Forget MFC CArchive/CFile...memberZac Howland3-Feb-03 6:18 
TBiker wrote:
This is not my idea of a clean implementation of serialization. You are assuming the use of pre-existing classes called CFile and CArchive. You are also assuming that flattening a data structure within a class only serves to save/recall from persistent storage. What if I want to send my class data across a TCP/IP socket or across other serial-based streams like Firewire or RS-232.
 
CFile and CArchive are both part of the MFC library, therefor it is safe to "assume" they exist.
 
While this demonstration of a Serialization mechanism does not show it, you can use CFile in conjunction with CArchive to send data through a RS-232 port. By changing the File class to CSocketFile, you can do it with TCP/IP as well. I haven't done it with USB and Firewire, but I am sure that all you have to do is derive you own classes from CFile and modify the setup functions (or you may be able to get away with just using CFile and opening up the USB/Firewire port).
 
TBiker wrote:
That's it. I just typed this design up so no guarantees I haven't made a mistake here but you get the general concept. Don't rely on MFC to do your design!
 
As long as you are writing code that you don't intend to port to UNIX, Linux, etc, there is nothing wrong with using MFC in your design. Why reinvent the wheel (which is what you just did in your example. You CSerialize class is almost equivalent to the CArchive class.)
 
The only issue with MFC is its portability. But if you are writing a Serialization routine, you are already screwed on that point anyway.
 
Zac

 
"If I create everything new, why would I want to delete anything?"
GeneralA General question about SerializationmemberNaresh25_cool23-Dec-02 1:19 
Hi,
 
Instead of the CFile object ( .dat file here) can other forms of storage medium like Magnetic tapes etc., be used to store the object data through serializaton process?
GeneralRe: A General question about SerializationsussAnonymous24-Dec-02 22:12 
不知道你在说什么!Confused | :confused:
GeneralRe: A General question about SerializationmemberRavi Bhavnani28-Dec-02 4:16 
The CFile object can be associated with a file on any medium (disk, tape, CD-RW). The operating system makes the physical location of the file transparent to the programmer.
 
/ravi
 
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
GeneralRe: A General question about SerializationmemberZac Howland3-Feb-03 6:20 
Naresh25_cool wrote:
Instead of the CFile object ( .dat file here) can other forms of storage medium like Magnetic tapes etc., be used to store the object data through serializaton process?
 
You can also use it to send/receive through a serial port.
 
Zac
 
"If I create everything new, why would I want to delete anything?"
GeneralA word of warningsussAnonymous18-Dec-02 21:51 
Saving data in binary format is often not a good idea. The reason is because you will often need to trouble shoot, ie. open the file in notepad to see just what's in there. If it's in binary format it's difficult to work with. The exception is when you have large amounts of data to save when it makes sense to reduce file size.
If you are serializing configuration data, I recommend to save it as XML.
GeneralMore warnings...memberPeter Weyzen8-Jan-03 11:14 
Sometimes serialization to a binary format is necessary -- like for doing network messaging...
 
(1) construct your serialization engine code so that it can be debugged. I have a serialization module that I use A LOT. In a debug mode I prepend each data item with a 1 byte header (a fixed marker), and 1 byte of type information. In debug mode, it also Asserts() when things don't match up right. It's additional work to do this, but it pays off!
 
(2) Avoid using overloaded methods. Streaming types automatically is nice. But I prefer that the programmer is conscious of what format he's creating, so I include methods like WriteBoolean(), WriteDWord(), WriteInt(), and to serialize nested objects, I have WriteObject(). It's more work to do this, but it makes the format clearer.
 
Other than that -- binary serialization is a great thing!
GeneralThis is good.memberMarc Clifton15-Dec-02 13:58 
I definitely appreciate your organization and diagrams. I'm don't particularly care for serialization, but your articles have given me pause to think about different ways serialization can be applied to my projects.
 
Marc
 
Help! I'm an AI running around in someone's f*cked up universe simulator.

sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus

GeneralSerializing Multiple Object Types To Same FilememberMangesh Sardesai4-Dec-02 21:01 
That was really nice article...
 
Can any one throw some light on serializtion / deserialization of objects of different type into / from the same file...?
 
Confused | :confused: Confused | :confused: Confused | :confused:
 
regards,
 
M$

GeneralRe: Serializing Multiple Object Types To Same FilememberRavi Bhavnani5-Dec-02 1:14 
Thanks. See part 3 for rules regarding serializing different types of objects to the same archive.
 
/ravi
 
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
GeneralRe: Serializing Multiple Object Types To Same FilememberMangesh Sardesai5-Dec-02 2:23 
Oops... It seems that I framed the question in incorrect way.
 
To be more explicit, consider the situation where i am required to serialize the objects of classes say CFOO1, CFOO2, CFOO3 in a sequence that is decided at runtime. I will be able to serialize the objects to the file using the method mentioned in the article.
 
Now if i want to deserialize the objects from a file how do i do it, as i dont know the sequence in which they were serialized?
 

Confused | :confused: Confused | :confused: Confused | :confused:
 
thanks and regards,
 
M$
GeneralRe: Serializing Multiple Object Types To Same FilememberRavi Bhavnani5-Dec-02 5:14 
You could do this by serializing another object, eg: CSerializationSequence that describes the sequence of objects to follow. This would support the run-time decision scenario you describe.
 
/ravi
 
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
Generali likememberumeflying4-Dec-02 20:25 
i like you program as i was a new programer,please give us some comment at free time.hehe,you don't know i mean.hehe
GeneralCObjectmemberKarstenK27-Nov-02 0:51 
wouldn´t it be better to use CObject as base class and than use
"Serialize( CArchive& ar )" syntax. To make it complete the operators should be mentioned/implemented. Cool | :cool:
 
Try this @ home. (B&B)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 25 Nov 2002
Article Copyright 2002 by Ravi Bhavnani
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid