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?
Handling checksums
Started by Wooh, Jul 29 2011 03:36 AM
1 reply to this topic
Sponsor:
#2 Members - Reputation: 1411
Posted 29 July 2011 - 04:40 AM
You only really have two options:
1. Read the file twice. The first time to verify the checksum, the second time to load it. This has a small possibility of going wrong if the first read works correctly, and the second one doesn't.
2. Read the whole file into temporary memory, verify the checksum, and then load from that memory into your game state. std::stringstream should make this fairly simple.
In most cases there's not much performance difference between the two since the operating system disc cache should make #1 almost as quick as #2.
1. Read the file twice. The first time to verify the checksum, the second time to load it. This has a small possibility of going wrong if the first read works correctly, and the second one doesn't.
2. Read the whole file into temporary memory, verify the checksum, and then load from that memory into your game state. std::stringstream should make this fairly simple.
In most cases there's not much performance difference between the two since the operating system disc cache should make #1 almost as quick as #2.






