c++ - How do you save objects with variable length members to a binary file usefully? -


i'm working c++ , writing budget program (i'm aware many available--it's learning project).

i want save call book object contains other objects such 'pages'. pages contain cashflows , entries. issue there can amount of entries or cashflows.

i have found lot of information on saving data text files not want do.

i have tried looking using boost library i've been told serialization might solution problem. i'm not entirely sure functions boost going or proper ways use boost.

most examples of binary files have seen objects have fixed size members. example, point might contain x value , y value both doubles. case simple use sizeof(point).

so, i'm either looking direct answers question or useful links information on how solve problem. please make sure links specific question.

i've posted same question on cplusplus

in general, there 2 methods store variable length records:

  1. store size integer first, followed data.
  2. store data, append sentinel character (or value) @ end.

c-style strings use 2nd option.
option one, number contains size of data.

optional fields

if considering relational database design optional fields, have 1 table known or fixed records , table containing option field record id.

a simpler route may go similar xml: field labels.
split object 2 sections: static fields , optional fields. static field section followed optional field section. optional field section contain field name, followed field data. read in field name value.

i suggest review design see if optional fields can eliminated. also, complex fields, have them read in own data.

storing binary data

if data shared between platforms, consider using ascii or textual representation.

read upon endianess , bit sizes. example 1 platform store binary representation least significant byte first , use 32 bits (4 bytes). receiving platform, 64-bit, significant byte first, have problems reading data directly , need convert; losing benefit binary storage.

similarly, floating point doesn't fare in binary either. there loss of precision when converting between floating point formats.

when using optional fields in binary, 1 use sentinel byte or number field id rather textual name.

also, data in textual format easier debug data in binary format.

consider using database

see at point worth using database?


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -