1

I'm looking forward to make my data usable during every restart of my program. The I am curious which is the best way to store to file than read back to program. i have been reading some stuff over the internet and the big question is XML or binary format? I'm still learning c++ i do not master it. the program's objects are of type string int int ... Which way do you recomand me to use and why?

One more thing does anyone know a good tutorial for this to binary or to XML?

Sorry for missing code part but i wanted to know some opinions of more advanced programmers than me. :P

Bogdan M.
  • 2,161
  • 6
  • 31
  • 53
  • 1
    What you're talking about is "object persistence". Search on those terms and you might find something useful. In general, though, the industry has preferred non-binary formats such as XML, JSON, or YAML because they can be edited and they don't have platform binary dependencies such as size of `int` or byte ordering. – Mike DeSimone May 22 '12 at 12:41

3 Answers3

3

I do not agree. There are plenty options available. Two more i name you here: 1) You may look to a file format called json which has an own website (some of us don't do). It claims to be a lightweight data-interchange format. 2) There is a file-format called csv. The usage of which was already discussed on stackoverflow here

Community
  • 1
  • 1
Matthias
  • 3,458
  • 4
  • 27
  • 46
3

In addition to Matthais comment:

I think the most obvious format is the correct on in your case, and that is just plain text.

Just serialize your data in plain text (often delimited by spaces). The benifit of PT is that it is human readable, human modifiable, easy to process using streams (>> tokenisation or boost tokeniser) and flexible and is much light weight than XML.

For example you might want to store

struct {
    std::string name;
    int age;
    double height;
};

you you would just write:

John 21 5.4
Bill 31 4.9

or whatever have you. This is always convient, for example name could contain two words so:

John Smith

And the tokeniser would split on spaces and try and parse smith as an int but that is an easy problem to fix using delimiters. Such as ""

111111
  • 15,686
  • 6
  • 47
  • 62
  • What tokeniser are you talkinga bout could you tell me the function/ library from c++? – Bogdan M. May 22 '12 at 13:11
  • The downsides of plain text are 1) you have to write the parser, 2) it's hard to expand later, and 3) error recovery is more difficult. – Mike DeSimone May 24 '12 at 13:34
  • @MikeDeSimone you only have to write a parse if the structure is complex, and most parsers are quite easy to write. 2). Not harder than binary, perhaps harder than XML 3). but possible, good luck explaining to the user why their XML or binary data is incorrect. – 111111 May 24 '12 at 14:29
2

Do you need robust behavior even when your process is killed prematurely (e.g. due to a power cut, hardware failure or a serious bug within your code itself)?

If so, consider an "embedded" database such as SQLite or MS SQL Server Compact (etc.). The transactional nature of these systems should ensure you can't end-up with corrupted data, that would later prevent your program from starting correctly.

Also, some file systems support transactions (e.g. transactional NTFS in Windows Vista or later).

Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
  • This looks intressting but complexity is a bit higher than i expected. I used Json in Python. i didn't knwo it is for c++ also thank you for informing me. :) – Bogdan M. May 22 '12 at 12:57
  • I'd say that "I need to work with far more data than reasonably fits in memory, and do a lot of cross-referencing on such" is a lot better reason to use databases than "robustness." – Mike DeSimone May 24 '12 at 13:32
  • @MikeDeSimone Well, all these are valid reasons in general, but the wording of the question suggests neither the size nor the need for "cross-referencing" are problem in this particular circumstance. And I did mention transactional file system as an alternative. – Branko Dimitrijevic May 24 '12 at 13:58