Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serializing an Array. More help please. 1

Status
Not open for further replies.

nyjil

Programmer
Jul 14, 2000
32
US
Thank you for your help in this topic, from an earlier question, but I'm still somewhat lost. I can serialize a single object now, with no problems (Thanks Thierry!). The problem is I want a whole array of objects to fit into a single file. I could serialize one object at a time into a single file, and then draw from each file and load it into an array, but my array is [80][80], two dimentional. This means I have 6400 objects, and I don't want to create 6400 files just to fill one array. Especially when I have about 120 different [80] x [80] arrays. That would make 768,000 objects in that many files! Possible, but not plausible and far too messy. So here's my question, how do I get an entire array into just one file?
 
Hi

Ok now that you have your array of object, you simply use a for ... next loop. Of course, the function that perform this is not within your object class.

Assume a class CMsg defined in a file msg.cpp

Say you have an array of CMsg declared in foo.cpp

CMsg m_amsg[10][10];

In the class CMSg you define the Serialize function (as I showed you before)

As one of the function of the foo.cpp, you will serialize the array:


for ( int n = 0; n < 10; n++)
{
for ( m = 0; m < 10; m++)
{
m_amsg[n][m].Serialize( ar);
}
}

HTH

Thierry


 
TGM is right.
There are some improvements you can do:
1.If you are serializing this way you should serialize first the dimensions of your array.
Then when you will de-serialize you will know what to put in the for loop. This way you can serialize different array sizes.
2.Make the Serialize function in a different class let's call it CArrayMsg. This way you will hide the details of implementation
3. If You are using MFC consider using also a CArray or a CObArray class.

HTH, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Worked like a charm. Thank you very much for your time and help. Thierry, your code examples did the trick wonderfully. Couldn't have done it without your help, or at least I'd still be scratching my head and fumbling through yet more text books. IonelBurtan, thanks for the ideas. That should clean things up nicely.

Thank You

Nyjil
;-)
 
Hi

IonalBurtam is right. Adding such information as the array size is a good protection against crach.

One thing I also systematically add to the serialization process is a file identifier and a fiel version number. The file identifier is always the first one and is a string like 'AppName AppDescription'.
When I read the file, I first check that the first element compares with the description.
Next is the file version. It allows you to modify the 'structure' of your serialized file transparantly for the user.
Supose you need something new in a new version:

if ( version == 1)
ar ...
else if ( version == 2)
ar ..
ar ..
ar ..


Happy that all is now good for you

Thierry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top