I want to save some data in a binary file. What I'm saving is the state of many instances of a certain class. Each class will be saving a string and 3 integer values. The length of the string and size of the integers are highly variable.
I'm choosing to use binary files to speed up the reading and writing process, and to avoid making the files readable. There's nothing sensitive in the data itself. It's just that the program relies heavily on this data staying intact, and I figured curious users would be less likely to mess with binary files that plain text.
Anyway, I don't know anything about writing binary files (I've only written text files), so I was hoping for some tips on effective binary writing.
My initial idea was to create a comma separated list of the data, and write that to the file. Would that be a good idea? Also, would the binary files be able to be portable? For example, if I wrote the files in a Linux environment, and then compiled my program in Windows, would it be able to read the Linux binary files? Or do I need to create separate binary files for any platform I want to support?
If you are worried about the file being corrupted, write a checksum of the data on the last line and validate it. You could use a well-known structured format such as XML or JSON. Boost has a library for just this sort of thing: http://www.boost.org/doc/libs/1_46_1/doc/html/property_tree.html