473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Saving class members to a binary file

I have some code that works it seems a bit of a hack. I want to know
if there is a more c++y way to do it.

Here's my code:-

class CMyClass{
public:
void Save(CFile fOut);
void Load(CFile fOut);

char *mPointerIDoNotWantTosave;
char *mAnotherPointerIDoNotWantTosave;
....
//start of data i want to save
int mDataStart;
....

int mDataEnd;
} ;
CMyClass::Save(CFile fOut)
{
int dataSize = (char *)mDataEnd - (char *)mDataStart) +
sizeof(mDataEnd);
fOut.Write(&mDataStart, dataSize);
}

I guess I could make my Save and Load functions write each member
individually, but that seems pretty tedious and makes changing the
class a pain. Is there a better way?

Jun 15 '07 #1
4 1911
ml****@gmail.com wrote:
I have some code that works it seems a bit of a hack. I want to know
if there is a more c++y way to do it.

Here's my code:-

class CMyClass{
public:
void Save(CFile fOut);
void Load(CFile fOut);

char *mPointerIDoNotWantTosave;
char *mAnotherPointerIDoNotWantTosave;
...
//start of data i want to save
int mDataStart;
...

int mDataEnd;
} ;
CMyClass::Save(CFile fOut)
{
int dataSize = (char *)mDataEnd - (char *)mDataStart) +
sizeof(mDataEnd);
fOut.Write(&mDataStart, dataSize);
Are you sure that between 'mDataStart' and the end of the chunk you
are saving there are no pointers to dynamic memory? It is possible
that what you're saving does not necessarily allow to restore the
object from file.
}

I guess I could make my Save and Load functions write each member
individually, but that seems pretty tedious and makes changing the
class a pain. Is there a better way?
Not really. "Serialization" is a rather complex issue, but the most
simple part of it is "store only the information that would allow
you to recreate the object when you run your program next time and
read the file; but no less".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 15 '07 #2
Are you sure that between 'mDataStart' and the end of the chunk you
are saving there are no pointers to dynamic memory? It is possible
that what you're saving does not necessarily allow to restore the
object from file.
}
Well I haven't placed any pointers in there - just a few ints, floats
and arrays therof. As I said it works, but I don't know if that is
gauranteed to work with a different compiler...
>
I guess I could make my Save and Load functions write each member
individually, but that seems pretty tedious and makes changing the
class a pain. Is there a better way?

Not really. "Serialization" is a rather complex issue, but the most
simple part of it is "store only the information that would allow
you to recreate the object when you run your program next time and
read the file; but no less".
K Thanks. I had a feeling that might be the answer. I think when I get
the class stable, I'll probably do that.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -

Jun 15 '07 #3
On 2007-06-15 19:19, ml****@gmail.com wrote:
I have some code that works it seems a bit of a hack. I want to know
if there is a more c++y way to do it.

Here's my code:-

class CMyClass{
public:
void Save(CFile fOut);
void Load(CFile fOut);

char *mPointerIDoNotWantTosave;
char *mAnotherPointerIDoNotWantTosave;
...
//start of data i want to save
int mDataStart;
...

int mDataEnd;
} ;
CMyClass::Save(CFile fOut)
{
int dataSize = (char *)mDataEnd - (char *)mDataStart) +
sizeof(mDataEnd);
fOut.Write(&mDataStart, dataSize);
}
That seems like a *really* bad idea, the compiler is allowed to put
padding between the members of the class and this padding might be
different on different platforms and even depending on the compiler
options you use.
I guess I could make my Save and Load functions write each member
individually, but that seems pretty tedious and makes changing the
class a pain. Is there a better way?
That depends on your definition of good, but writing each member is
probably the best. There are however other solutions out there, try
searching for serialization..

--
Erik Wikström
Jun 15 '07 #4
Erik Wikström wrote:
That seems like a *really* bad idea, the compiler is allowed to put
padding between the members of the class and this padding might be
different on different platforms and even depending on the compiler
options you use.
My guess is that his intention is to store the contents of the
class only temporarily and the stored data to be read only by the
exact same program in the exact same computer. I think that if that's
the case, it should work ok, although it is indeed a bit of a dirty
hack.
Jun 17 '07 #5

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

Similar topics

7
by: G-Factor | last post by:
Hi all I've just started learning about saving files. I got bit of a problem. The following code gives me an error about incompatible types. (Cannot covert from class character to char *). I...
3
by: hall | last post by:
I have a problem with my design of a templatized class. I'm trying to figure out how to load and save the data inside it, but can't. My class looks like this ------------------------------------...
4
by: Michael Kennedy [UB] | last post by:
Hi Everyone, I have this multithreaded C# windows forms application which does a lot of image processing. Occasionally, I get the following error: A generic error occurred in GDI+....
4
by: Jerivix Entadi | last post by:
I'm attempting to create an application to work with a fluid database of people. I'm doing this in a command line, text-based manner. I need to know two things: 1) How do I save data, perhaps a...
12
by: imme929 | last post by:
How do I do it? Nothing in the books is helpful. I need to save a structure with different data types.
3
by: Peter Oliphant | last post by:
Trying to save/load a class instance to a file is tough. The reason is because there is no way to preserve its type other than by conventon. For eample, one can save all the states of the members...
4
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my...
5
venere
by: venere | last post by:
Hi everybody, Well, suppose an inheritance relation between Show, Movie and Play, with Show being an abstract class. I save (using append) Movie and Play objects in two different binary files. ...
8
by: zl2k | last post by:
hi, there Here is a simplified piece of code of my program, it compiles and runs fine. However, valgrind shows it has uninitialized problem. What I am doing wrong? #ifndef DATA2_H #define...
0
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...
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,...
0
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...
1
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...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.