I am programming a board game and I have to make a binary file called "write" that always writes the current playfield. It contains information like the active player, the biggest and the smallest x/y coordinate. Fields without tokens on it should have the value zero. It has to be done so that the program can be tested automaticly easier.
I thought about making a class called CurrentField that I write in my binary file after every turn, but I'm not sure how I can ensure that everything will be written in the place where it needs to be (we have to declare a specific header and certain things are expected to be in a certain offset).
Any ideas about that? Good idea, or is there a better way to do this?
Edit: And does anyone know a way of testing what really has been written to the binary file? Edit2: It has to be solved with c++ standart libraries. The performance is not that important since it will not be a large file. The idea of writting only if a command is entered is good.
There are at least two good libraries that can help you with that.
Boost Serialization is designed to serialize some arbitrary structures. It includes some good concepts as versioning (you may opt to change the stored data in an update of your game and leave the former game state intact) and archive
s (what if you'd like to change the file format at all?
Boost Spirit is designed to parse and serialize with great speed any text and binary protocols. It works a lot faster.
You can workaround any issues just storing the memory location in file with file-memory mapping. Be careful with pointers, though.
Check if you really need to do this on every step, as filesystem calls are quite slow. At least, do it asynchronously (look for Boost Asio).