Original Post
I want to have a checksum in a savefile so that I can check if the savefile is not corrupted. I'm a bit unsure how to handle this the best way. At the moment I have a wrapper so that all data that is written is written to file using std::ofstream and also passed through the checksum calculation so I can later write the checksum at the end of the file.
When reading the data back I could use a similar approach letting all read data pass through the checksum calculation and when that is done I can compare the checksum. The problem with this approach is that the read routines are used while restoring the gamestate and at the end when it's time to validate the checksum I have already constructed the whole gamestate. To me this feels wrong. I feel I should validate the checksum first but then I will have to read through the file twice, once for calculating the checksum, and once when restoring the gamestate.
So, what is the best way of handling checksums, especially while reading?
When reading the data back I could use a similar approach letting all read data pass through the checksum calculation and when that is done I can compare the checksum. The problem with this approach is that the read routines are used while restoring the gamestate and at the end when it's time to validate the checksum I have already constructed the whole gamestate. To me this feels wrong. I feel I should validate the checksum first but then I will have to read through the file twice, once for calculating the checksum, and once when restoring the gamestate.
So, what is the best way of handling checksums, especially while reading?