Binary save files

Started by
4 comments, last by jwezorek 10 years, 10 months ago

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.

If you get near a point, make it!
Advertisement

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 serialization 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 Boost Serialization but I doubt you have knowledge of sufficient to work with it.

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!

If you get near a point, make it!

std::string is not a POD type. A pointer is.

But I thought that POD meant Plain Old Data, I thought that was the opposite of a pointer!?

If you get near a point, make it!

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.


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]

This topic is closed to new replies.

Advertisement