Jump to content

  • Log In with Google      Sign In   
  • Create Account


Binary save files


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Ubermeowmix   Members   -  Reputation: 311

Like
0Likes
Like

Posted 17 June 2013 - 02:49 AM

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!

Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 3811

Like
2Likes
Like

Posted 17 June 2013 - 03:30 AM

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.


Edited by BitMaster, 17 June 2013 - 03:32 AM.


#3 Ubermeowmix   Members   -  Reputation: 311

Like
0Likes
Like

Posted 17 June 2013 - 04:46 AM

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!

#4 Ubermeowmix   Members   -  Reputation: 311

Like
0Likes
Like

Posted 17 June 2013 - 04:47 AM

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!

#5 Hodgman   Moderators   -  Reputation: 28667

Like
2Likes
Like

Posted 17 June 2013 - 05:01 AM

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.



#6 jwezorek   Crossbones+   -  Reputation: 1773

Like
2Likes
Like

Posted 17 June 2013 - 10:51 AM


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, 17 June 2013 - 10:54 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS