Binary Files

Started by
13 comments, last by TridDiute 21 years, 6 months ago
You can save classes and structs to a file. Example:

bool CSettings::Save()
{
FILE* fileDescriptor;

fileDescriptor = fopen( "classfile", "wb" );

if( fileDescriptor )
{
fwrite( this, sizeof( CSettings ), 1, fileDescriptor );

fclose( fileDescriptor );

return true;
}
return false;
}

Easy as pie. Then to load it:

bool CSettings::Open()
{
FILE* fileDescriptor;

fileDescriptor = fopen( "classfile", "rb" );

if( fileDescriptor )
{
fread( this, sizeof( CSettings ), 1, fileDescriptor );

fclose( fileDescriptor );

return true;
}
return false;
}

Amazing, isn''t it?
Advertisement
Mulligan: actually, no. You must provide functions that stream in and out all the member data member by member. Refer to Fruny''s post as to why you should do this

Amazing, isn''t it?


Only if you believe illusions, Mulligan... Loading/Saving classes in that fashion has a tendancy to cause massive heartburn and chronic loss of sleep ;-)

--- "A penny saved never boils."
quote:Original post by Mulligan
You can save classes and structs to a file. Example:


Your code is an abomination.

a) If your class/structs contains pointers, you''re only saving the value of the pointer, not the data it points to. And pointers CANNOT be persisted across address spaces. This alone will make your code fail pathetically

b) You are happily overwriting the class data members, never destroying. Enjoy your resource (memory, files, handles ...) leaks.

c) Similarly, you are never constructing the data members, nor the base classes, properly. If one of my data member was a FILE* (since you''re using one, you must understand how it works), I would want to call fopen() on it, not just slap an arbitrary value, wouldn''t I ?

d) If your class had a virtual function, or inherited from a class with one - notably a virtual destructor, you just trashed the virtual function table. Real smart !


Classes and structs cannot be read/written as-is to a stream unless you take a number of precautions :

- no pointers
- no references
- no objects with private data
- no objects with constructors or copy constructors
- no objects with destructors
- no objects with virtual functions
- RTTI is disabled
- no base class or member violating one of the points above

At which point you have what the C++ standard names a POD-class or POD-struct, which can be read/written directly to a stream, so long as you preserve its internal layout, including, but not limited to :

- endianness
- data alignment
- ordering of member variables
- ordering of base classes

Only with those restrictions adhered to will your code have a shade of a chance of working.

quote:Amazing, isn''t it?


Amazingly incorrect, yes.


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
so i write the members out member by member .. ok;

- pointers (you''d save the value of the pointer, not the data)

how can i avoid this ? , as i use pointers a lot ;
dudu ...

This topic is closed to new replies.

Advertisement