473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

serialize a struct

Hi,

I have a structure like this:
struct TEST {
int x, y, z;
string str;
vector<floatvFloat;
};
I need to serialize an instance of this struct into a stream of bytes
(its for insertion into a 3rd party file). This 3rd party library has a

function to insert the serialized buffer into their files that looks
like:
SetPrivateData(unsigned char *pData, int nSize);
It was suggested I do the following:
TEST t;
// how much space do we need?
int bufsize = sizeof(int)* 3 + sizeof(float)*t.vFloat.size() +
t.str.length()+1;
// create and clear the buffer
unsigned char *buf = new unsigned char[bufsize];
memset(buf, 0, bufsize);
unsigned char *pBuf = buf;
int *pIntBuf = (int *) buf;
// copy in the ints
*pIntBuf = t.x;
pIntBuf++;
*pIntBuf = t.y;
pIntBuf++;
*pIntBuf = t.z;
*pIntBuf++;
// copy in the floats
pBuf = (unsigned char *)pIntBuf;
vector<float>::pointer floatptr = &(t.vFloat[0]);
memcpy(pBuf, floatptr, t.vFloat.size()*sizeof(float);
pBuf += t.vFloat.size()*sizeof(float);
// copy in the string
strcpy(pBuf, t.str.c_str());
But I'm not sure if this is working correctly, the above executes but
dies when I try printing any portion of it to screen to examine the
contents using printf(). Is the above doing what I need it to do?
Thanks

Sep 3 '06 #1
1 3760
On 3 Sep 2006 11:15:24 -0700 in comp.lang.c++, "markww"
<ma****@gmail.comwrote,
>But I'm not sure if this is working correctly, the above executes but
dies when I try printing any portion of it to screen to examine the
contents using printf(). Is the above doing what I need it to do?
I find the style of the mentioned code to be a step backward for
someone who is properly using string and vector, but let's not get
into that (unless you want to.) The actual problem I notice is that
you do not write out the vector size before the elements, and so
will have some difficulty figuring out how many elements to read
back later.

As for printf, I find it curious that the thing that is not working
for you is the ONE thing you did not post so that we could look at
it. Instead of printf, try e.g.

std::cerr << std::hex;
std::copy(buf, pbuf, std::ostream_iterator<int>(std::cerr, " "));

Note the <intis significant.

Sep 3 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Papa.Legba.666 | last post by:
I use a PHP based blog system and am considering writing some offline tools. Does anyone know of a C++ class or function which can un/serialze() data compatibly with PHP ? Thanks in advance...
16
by: Corne' Cornelius | last post by:
Hi, I need to save a (complex) struct which has pointer members, to a file, and load it again later. The actual memory which the pointers point to must be saved, not the memory address. Will...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
5
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...
0
by: CowPad | last post by:
Hello All, I have a problem with serializing structs so I made a small application here to demonstrate. Hopefully someone can explain this strange problem. I have a Windows (Forms)...
3
by: Frank H. | last post by:
Hi, in my webservice I have a Webmethod wich returns a list of Objects to my C++ Application. C# code: public class Contact { public string FirstName;
0
by: banduraj | last post by:
I get an System.InvalidOperationException when trying to Serialize a struct that has a type refrenced from another library. The exception is: System.InvalidOperationException was unhandled...
7
by: markww | last post by:
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;
1
by: markww | last post by:
Hi, I have a structure like this: struct TEST { int x, y, z; string str; vector<floatvFloat; };
3
by: =?Utf-8?B?Q2VjY28gKElUQSk=?= | last post by:
Hi all, I need to know if there's a way to serialize a windows form. I included the /<serializablein the "myclassform" declaration, but I received an error because the windows.form class isn't...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...

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.