serialize a structure

M

markww

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.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
 
M

mlimber

markww said:
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.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-lite/serialization.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-lite/how-to-post.html#faq-5.9).

Cheers! --M
 
G

Gernot Frisch

markww said:
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;

std::eek:stream& operator << (std::eek:stream& os)
{
os << x << y << z << str1 << str1 << str2;
os << vfloats1.size();
for (int i=0; i<vflaots1.size; ++i) os << vfloats1;
os << vfloats2.size();
for (int i=0; i<vflaots2.size; ++i) os << vfloats2;
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.
 
M

markww

Gernot said:
markww said:
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;

std::eek:stream& operator << (std::eek:stream& os)
{
os << x << y << z << str1 << str1 << str2;
os << vfloats1.size();
for (int i=0; i<vflaots1.size; ++i) os << vfloats1;
os << vfloats2.size();
for (int i=0; i<vflaots2.size; ++i) os << vfloats2;
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
 
M

mlimber

markww said:
Gernot said:
markww said:
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;

std::eek:stream& operator << (std::eek:stream& os)
{
os << x << y << z << str1 << str1 << str2;
os << vfloats1.size();
for (int i=0; i<vflaots1.size; ++i) os << vfloats1;
os << vfloats2.size();
for (int i=0; i<vflaots2.size; ++i) os << vfloats2;
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-lite/serialization.html#faq-36.7),
you would use a similarly constructed extraction operator for a
std::istream.

Cheers! --M
 
M

mlimber

mlimber said:
markww said:
Gernot said:
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;

std::eek:stream& operator << (std::eek:stream& os)
{
os << x << y << z << str1 << str1 << str2;
os << vfloats1.size();
for (int i=0; i<vflaots1.size; ++i) os << vfloats1;
os << vfloats2.size();
for (int i=0; i<vflaots2.size; ++i) os << vfloats2;
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-lite/serialization.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
 
M

markww

mlimber said:
mlimber said:
markww said:
Gernot Frisch wrote:
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;

std::eek:stream& operator << (std::eek:stream& os)
{
os << x << y << z << str1 << str1 << str2;
os << vfloats1.size();
for (int i=0; i<vflaots1.size; ++i) os << vfloats1;
os << vfloats2.size();
for (int i=0; i<vflaots2.size; ++i) os << vfloats2;
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-lite/serialization.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
 
J

Jim Langston

markww said:
mlimber said:
markww wrote:
Gernot Frisch wrote:
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;

std::eek:stream& operator << (std::eek:stream& os)
{
os << x << y << z << str1 << str1 << str2;
os << vfloats1.size();
for (int i=0; i<vflaots1.size; ++i) os << vfloats1;
os << vfloats2.size();
for (int i=0; i<vflaots2.size; ++i) os << vfloats2;
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-lite/serialization.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top