468,457 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

serialize a structure

Hi,

I have a data structure that looks like this:

struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfloats1;
vector<floatvfloats2;
}

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.s ize()|vfloats1|vfloats2.size()|vfloats|

but I'm wondering if there is a simpler way of 'serializing' the
structure into a stream of bytes? Any ideas would be great. I am also
using MFC by the way, I see they have their own serialization scheme,
that is also a possibility for me to take advantage of,

Thanks

Aug 28 '06 #1
7 6829
markww wrote:
Hi,

I have a data structure that looks like this:

struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfloats1;
vector<floatvfloats2;
}

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.s ize()|vfloats1|vfloats2.size()|vfloats|

but I'm wondering if there is a simpler way of 'serializing' the
structure into a stream of bytes? Any ideas would be great. I am also
using MFC by the way, I see they have their own serialization scheme,
that is also a possibility for me to take advantage of,

Thanks
See these FAQs:

http://www.parashift.com/c++-faq-lit...alization.html

You might want to consider Boost.Serialization:

http://boost.org/libs/serialization/doc/index.html

As for MFC, you'll have to ask on a Microsoft newsgroup (see the list
at http://www.parashift.com/c++-faq-lit....html#faq-5.9).

Cheers! --M

Aug 28 '06 #2

"markww" <ma****@gmail.comschrieb im Newsbeitrag
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi,

I have a data structure that looks like this:

struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfloats1;
vector<floatvfloats2;
std::ostream& operator << (std::ostream& os)
{
os << x << y << z << str1 << str1 << str2;
os << vfloats1.size();
for (int i=0; i<vflaots1.size; ++i) os << vfloats1[i];
os << vfloats2.size();
for (int i=0; i<vflaots2.size; ++i) os << vfloats2[i];
return os;
}
}

You might get into trouble with the strings, though, since some
delimiter characters as '\n' within a std::string can't be read in
this way.

Aug 28 '06 #3

Gernot Frisch wrote:
"markww" <ma****@gmail.comschrieb im Newsbeitrag
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi,

I have a data structure that looks like this:

struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfloats1;
vector<floatvfloats2;

std::ostream& operator << (std::ostream& os)
{
os << x << y << z << str1 << str1 << str2;
os << vfloats1.size();
for (int i=0; i<vflaots1.size; ++i) os << vfloats1[i];
os << vfloats2.size();
for (int i=0; i<vflaots2.size; ++i) os << vfloats2[i];
return os;
}
}


You might get into trouble with the strings, though, since some
delimiter characters as '\n' within a std::string can't be read in
this way.
Gernot, how would I read that back in?

Thanks

Aug 28 '06 #4
markww wrote:
Gernot Frisch wrote:
"markww" <ma****@gmail.comschrieb im Newsbeitrag
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi,
>
I have a data structure that looks like this:
>
struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfloats1;
vector<floatvfloats2;
std::ostream& operator << (std::ostream& os)
{
os << x << y << z << str1 << str1 << str2;
os << vfloats1.size();
for (int i=0; i<vflaots1.size; ++i) os << vfloats1[i];
os << vfloats2.size();
for (int i=0; i<vflaots2.size; ++i) os << vfloats2[i];
return os;
}
}

You might get into trouble with the strings, though, since some
delimiter characters as '\n' within a std::string can't be read in
this way.

Gernot, how would I read that back in?
As the FAQs say
(http://www.parashift.com/c++-faq-lit...html#faq-36.7),
you would use a similarly constructed extraction operator for a
std::istream.

Cheers! --M

Aug 28 '06 #5

mlimber wrote:
markww wrote:
Gernot Frisch wrote:
"markww" <ma****@gmail.comschrieb im Newsbeitrag
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi,

I have a data structure that looks like this:

struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfloats1;
vector<floatvfloats2;
>
std::ostream& operator << (std::ostream& os)
{
os << x << y << z << str1 << str1 << str2;
os << vfloats1.size();
for (int i=0; i<vflaots1.size; ++i) os << vfloats1[i];
os << vfloats2.size();
for (int i=0; i<vflaots2.size; ++i) os << vfloats2[i];
return os;
}
>
}
>
>
You might get into trouble with the strings, though, since some
delimiter characters as '\n' within a std::string can't be read in
this way.
Gernot, how would I read that back in?

As the FAQs say
(http://www.parashift.com/c++-faq-lit...html#faq-36.7),
you would use a similarly constructed extraction operator for a
std::istream.
PS, you'll need to add some sort of delimeter between elements, whether
a space, comma, new line, or whatever. Otherwise the unserializing
won't be possible.

Cheers! --M

Aug 28 '06 #6

mlimber wrote:
mlimber wrote:
markww wrote:
Gernot Frisch wrote:
"markww" <ma****@gmail.comschrieb im Newsbeitrag
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi,
>
I have a data structure that looks like this:
>
struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfloats1;
vector<floatvfloats2;

std::ostream& operator << (std::ostream& os)
{
os << x << y << z << str1 << str1 << str2;
os << vfloats1.size();
for (int i=0; i<vflaots1.size; ++i) os << vfloats1[i];
os << vfloats2.size();
for (int i=0; i<vflaots2.size; ++i) os << vfloats2[i];
return os;
}

}


You might get into trouble with the strings, though, since some
delimiter characters as '\n' within a std::string can't be read in
this way.
>
Gernot, how would I read that back in?
As the FAQs say
(http://www.parashift.com/c++-faq-lit...html#faq-36.7),
you would use a similarly constructed extraction operator for a
std::istream.

PS, you'll need to add some sort of delimeter between elements, whether
a space, comma, new line, or whatever. Otherwise the unserializing
won't be possible.

Cheers! --M
Oh dear, this is going to be painful. If I have 10000 elements in my
float vectors, that's a lot of delimitation.

Thanks guys,
Mark

Aug 28 '06 #7
"markww" <ma****@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
>
mlimber wrote:
>mlimber wrote:
markww wrote:
Gernot Frisch wrote:
"markww" <ma****@gmail.comschrieb im Newsbeitrag
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi,
>
I have a data structure that looks like this:
>
struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfloats1;
vector<floatvfloats2;

std::ostream& operator << (std::ostream& os)
{
os << x << y << z << str1 << str1 << str2;
os << vfloats1.size();
for (int i=0; i<vflaots1.size; ++i) os << vfloats1[i];
os << vfloats2.size();
for (int i=0; i<vflaots2.size; ++i) os << vfloats2[i];
return os;
}

}
You might get into trouble with the strings, though, since some
delimiter characters as '\n' within a std::string can't be read in
this way.

Gernot, how would I read that back in?

As the FAQs say
(http://www.parashift.com/c++-faq-lit...html#faq-36.7),
you would use a similarly constructed extraction operator for a
std::istream.

PS, you'll need to add some sort of delimeter between elements, whether
a space, comma, new line, or whatever. Otherwise the unserializing
won't be possible.

Cheers! --M

Oh dear, this is going to be painful. If I have 10000 elements in my
float vectors, that's a lot of delimitation.
It's 1000 characters which isnt' much.

I would use spaces to delimit them since that works easily with reading them
back in.

Consider, also, that you don't actually need to store the size of the
vectors.
x y z
string1
string2
vec11 vec12 vec13 vec14 vec15
vec21 vec22 vec23 vec24 vec25 vec26 vec27

Now in your operator<< you read 3 ints, throw the rest of the line away.
read a line - store in string1
read a line - strong in string2
read a line - process through stringstream to get elements out
std::string InputString;
std::getline( Inputstream, InputString );
std::stringstream LineStream;
LineStream << InputString;
float Value;
while ( LineStream >Value )
vector1.push_back( Value );
there, that processes one vector. Now do the same for the second.

Personally, I find the delimitators that istream works with the easiest to
use.

>
Thanks guys,
Mark

Aug 29 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by vince | last post: by
3 posts views Thread by Pol Bawin | last post: by
3 posts views Thread by MAY | last post: by
5 posts views Thread by objectref | last post: by
2 posts views Thread by Scott Collier | last post: by
18 posts views Thread by yusuf | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.