Sign in to follow this  
Ubermeowmix

Binary save files

Recommended Posts

Ubermeowmix    969

Hi all,

 

Im having trouble visualizing how binary files save the data from your program, especially with the following setup:

 

main program variables int, string.

 

Vector of classes, each with POD inside them (correct me if i'm wrong but this is just int, char, string. No pointers etc involved), and inside those classes if I want to use inherited classes how are these saved?

 

I just don't get how the binary file is laid out and how you can structure it if at all.

Share this post


Link to post
Share on other sites
BitMaster    8651

std::string is not a POD type. A pointer is. You cannot just write a pointer to a file though. A simple way to serialize a string would be to write an uint32_t to the file first which contains the length of the string followed by length chars as returned by myString.data().

If the type of objects you try to write to the file is not known at compile time then you first need to write something to the file identifying the type, be it an enum or an actual class name (the reader must then be able to create an instance of the appropriate class and deserialize it).

 

The whole issue of [url=http://en.wikipedia.org/wiki/Serialization]serialization[/url] is an extremely complex issue. There is no "this fits all" solution either. It can be quiet simple and become arbitrarily complex depending on what exactly needs to be serialized.

 

There is of course [url=http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/index.html]Boost Serialization[/url] but I doubt you have knowledge of sufficient to work with it.

Edited by BitMaster

Share this post


Link to post
Share on other sites
Ubermeowmix    969

Yeah I've just found a really good site explaining it:

 

http://www.parashift.com/c++-faq-lite/serialize-no-inherit-no-ptrs.html

 

Thanks for the input though much appreciated.

 

I'm quite a visual person, I need things to be quantified tangibly so this is a real struggle on the advanced processes needed to do something that at first sounds simple but on investigation is far from it!

Share this post


Link to post
Share on other sites
Hodgman    51234

POD objects are "just a pile of bits". Non-POD objects have behaviours, like constructors, destructors, operators, etc.

 

An integer is a pile of bits. A pointer to something is basically the same as an integer - it is also just a pile of bits, but it's interpreted differently. Instead of it being interpreted as a number (like int is), it's interpreted as an address within RAM.

 

The objects that exists at that address (i.e. the thing that is pointed to by the pointer) might be non-POD, but the actual pointer variable itself is just plain old data.

Share this post


Link to post
Share on other sites
jwezorek    2663

Vector of classes, each with POD inside them (correct me if i'm wrong but this is just int, char, string. No pointers etc involved), and inside those classes if I want to use inherited classes how are these saved?

 

By inventing a binary file format and implementing functionality that writes a representation of your objects to this format and functionality that creates your objects from a binary file in the format you invented. There is nothing magical about this. You have functions for writing bytes and functions for reading bytes. Think about how to use these functions to store and retrieve the state you need to be persistent.

 

Say you have a vector of pointers to instances of Foobar objects, where Foobar is an abstract base class from which a concrete Foo class a concrete Bar class inherit. Say each foo and each bar contains only an integer ID. You could define a file format like this:

 

[number, n, of Foobars in vector - 4 bytes]

[ type of first Foobar object, Foo or Bar -- 1 byte]

[ id of first  Foobar object -- 4 bytes]

[ type of 2nd Foobar object-- 1 byte]

[ id of 2nd Foobar object -- 4 bytes]

...

[ type of nth Foobar object-- 1 byte]

[ id of nth Foobar object -- 4 bytes]

Edited by jwezorek

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this