473,666 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<floatvfl oats1;
vector<floatvfl oats2;
}

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.leng th|str1|str2.le ngth|str2|vfloa ts1.size()|vflo ats1|vfloats2.s ize()|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 7299
markww wrote:
Hi,

I have a data structure that looks like this:

struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfl oats1;
vector<floatvfl oats2;
}

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.leng th|str1|str2.le ngth|str2|vfloa ts1.size()|vflo ats1|vfloats2.s ize()|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.Serializa tion:

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.c omschrieb im Newsbeitrag
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hi,

I have a data structure that looks like this:

struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfl oats1;
vector<floatvfl oats2;
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.c omschrieb im Newsbeitrag
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hi,

I have a data structure that looks like this:

struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfl oats1;
vector<floatvfl oats2;

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.c omschrieb im Newsbeitrag
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hi,
>
I have a data structure that looks like this:
>
struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfl oats1;
vector<floatvfl oats2;
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.c omschrieb im Newsbeitrag
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hi,

I have a data structure that looks like this:

struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfl oats1;
vector<floatvfl oats2;
>
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.c omschrieb im Newsbeitrag
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hi,
>
I have a data structure that looks like this:
>
struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfl oats1;
vector<floatvfl oats2;

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.c omwrote in message
news:11******** **************@ 75g2000cwc.goog legroups.com...
>
mlimber wrote:
>mlimber wrote:
markww wrote:
Gernot Frisch wrote:
"markww" <ma****@gmail.c omschrieb im Newsbeitrag
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hi,
>
I have a data structure that looks like this:
>
struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfl oats1;
vector<floatvfl oats2;

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::stringstre am LineStream;
LineStream << InputString;
float Value;
while ( LineStream >Value )
vector1.push_ba ck( 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
14296
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
3
2301
by: Pol Bawin | last post by:
A class has a private field of type IWizard (An interface) and a public property to access it. When I try to serialize the Geometry class in XML, i have an error but it works in Binary Can you help me public class Geometry {
5
2134
by: Arjen | last post by:
Hello, I have an array "aPeople" with people objects which I serialize like this: XmlSerializer x = new XmlSerializer( typeof(People) ); TextWriter writer = new StreamWriter( "data.xml" ); x.Serialize( writer, aPeople ); Now I have also an array "aAnimal" with animal objects. Is there a way to put this in the same data.xml file?
3
10381
by: MAY | last post by:
Hi, I have a problem about serialize the form controls. I wrote a test program to test serialize a from but fail (->An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll) . Thx in advance. Here is the part of the code: Regards MAY
0
3437
by: meh | last post by:
Still have not been able to convert this. More importently.....Is this sample going about it the right way??? tia meh Here is the vb code.........I keep looking at older vb.net projects to try and figure out how to teach myself C# based on my old vb code but it's been more difficult than I first thought. I'm finding that it would be better if didn't know any previous programming language.
5
2373
by: objectref | last post by:
Hi to all, let's say i have a form with 2 buttons and 3 textboxes on it. Is there a way to Serialize the form so when i de-Serialize and cast it to a Form, i will get back the original form with the textboxes filled with their original values ?? I have built an app that i want to have the values of many-many forms' controls on the disk and then all the values back to the forms,
2
1840
by: Scott Collier | last post by:
I would like to serialize a custom structure eg. Private Structure Dog Dim DogName As String Dim DogAge As Integer Dim Fleas As ArrayList End Structure I am using a BinaryFormatter() to serialize the data. Each Dog is added to a HashTable, and then the final HashTable is serialized... well that is the
18
2331
by: yusuf | last post by:
I basically want to be able to store and retrieve a tree structure in greasemonkey. Unfortunately the GM_setValue and GM_getValue functions only take string key value pairs. Is there a native javascript function that will serialize the object so that it can be stored and retrieved as strings? Thanks.
4
2253
by: teddysnips | last post by:
I would like to serialize a class to XML. The format of the XML is fixed (see below). It only needs to be serialized - it is read by another application. I know I can do it manually using XMLTextWriter - e.g. Dim xwriter = New XmlTextWriter("C:\Projects\Formflo\WindowsService\NDde\test.xml", System.Text.Encoding.UTF8) xwriter.Formatting = Formatting.Indented
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.