Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

serialize a structure (with MFC)?

206 views
Skip to first unread message

markww

unread,
Aug 28, 2006, 10:19:48 AM8/28/06
to
Hi,

I have a data structure that looks like this:


struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<float> vfloats1;
vector<float> vfloats2;
}


I'm trying to save this structure into a 3rd party file to be read back

later. I was told I could serialize this structure into a stream of
bytes and then dump it into the file.


For this type of stuff I'm used to creating a delimited version of the
struct, using pipes for example:


x|y|z|str1.length|str1|str2.length|str2|vfloats1.size()|vfloats1|vfloats2.s­ize()|vfloats|

but I'm wondering if there is a simpler way of 'serializing' the
structure into a stream of bytes? I am using MFC in my application and
was wondering if maybe the MFC method of serialization could work? I
just don't understand how I would set that up, because basically at the
end of serialization I need to hand the 3rd party function something
like this:

3rdParty.SetByteStream(MySerializedStruct);

Thanks

Ajay Kalra

unread,
Aug 28, 2006, 10:42:44 AM8/28/06
to
In MFC, a CObject derived class can implement its own serialization and
take advantage of what MFC provides. Look here for an example:

http://www.pluralsight.com/articlecontent/cpprep1295.htm

---
Ajay

Tom Serface

unread,
Aug 28, 2006, 11:19:09 AM8/28/06
to
Since our struct contains other object I think you would be wise to send
this in some sort of readable format such as comma delimited (convert ints
to ASCII). You would also need some sort of indication as to how may floats
are in the vector, etc. This would make it tons easier to debug and
maintain.

Tom

"markww" <mar...@gmail.com> wrote in message
news:1156774788.6...@b28g2000cwb.googlegroups.com...

markww

unread,
Aug 28, 2006, 12:40:40 PM8/28/06
to

Using MFC's serialization techinique, we store to a class called
CArchive - I'm wondering if it's possible to store to some binary block
instead because I need to serialize the contents of my struct into a
stream of bytes to insert into another 3rd party file?

Thanks

Ajay Kalra

unread,
Aug 28, 2006, 12:57:05 PM8/28/06
to
>
> Using MFC's serialization techinique, we store to a class called
> CArchive - I'm wondering if it's possible to store to some binary block
> instead because I need to serialize the contents of my struct into a
> stream of bytes to insert into another 3rd party file?
>


The link I provided earlier shows how to serialize any data. As long as
3rd party provides access to the stream, you should be fine. Do these
guys give you a CArchive object to which you want to serialize your
struct.

---
Ajay

markww

unread,
Aug 28, 2006, 1:01:37 PM8/28/06
to

Hi Ajay,

No they dont give access to a CArchive - all they give you is this
function:

SetData(const char *pszData);

I guess they just want me to serialize it as text :(. I can't figure
out how it would be possible to make CArchive usable with that!

Tom Serface

unread,
Aug 28, 2006, 1:57:42 PM8/28/06
to
Maybe this article series will help you out:

http://www.codeproject.com/cpp/serialization_primer1.asp?df=100&forumid=3305&exp=0&select=406879

Tom

"markww" <mar...@gmail.com> wrote in message

news:1156784497.7...@b28g2000cwb.googlegroups.com...

Ajay Kalra

unread,
Aug 28, 2006, 3:55:24 PM8/28/06
to
> No they dont give access to a CArchive - all they give you is this
> function:
>
> SetData(const char *pszData);
>
> I guess they just want me to serialize it as text :(. I can't figure
> out how it would be possible to make CArchive usable with that!

It does not appear you can use CArchive. It looks like that the 3rd
party wants only char data and it will serialize it itself. Your
original post had indicated that you are serializing it. I am not sure
if I understand the problem completely.

---
Ajay

Scott McPhillips [MVP]

unread,
Aug 28, 2006, 4:42:13 PM8/28/06
to
markww wrote:
> Hi Ajay,
>
> No they dont give access to a CArchive - all they give you is this
> function:
>
> SetData(const char *pszData);
>
> I guess they just want me to serialize it as text :(. I can't figure
> out how it would be possible to make CArchive usable with that!
>

CArchive is only usable if the receiving code is also using CArchive.
Forget CArchive.

--
Scott McPhillips [VC++ MVP]

Tom Serface

unread,
Aug 28, 2006, 5:30:32 PM8/28/06
to
Mark,

If you want to serialize the data as "text" you may want to consider using
XML to set and parse the data. I think you could use a CArchive to do that
since you can override the functionality of how the data is written, but I'm
not sure that would be the easiest way to do it. I'd probably just write my
own XML parser routines to read and store the data. The nice thing about
this method is that you can read it easily to see if it is correct.

Tom

"markww" <mar...@gmail.com> wrote in message

news:1156784497.7...@b28g2000cwb.googlegroups.com...

0 new messages