lets suppose I create a game maybe a console game or even a GUI game using SDL or SFML,how would you save the highscore(make it persistent),the obvious choice would be to use an ifstream and ofstream and save it to a .txt file,
but this isn't suffice as the user could cheat and edit that score directly in the text file itself. how do games implement this,I have not seen any information online on how to do this
It can be tempting to modify text files so making it a little more difficult is probably a good idea, but personally I don't think it's worth doing anything extremely advanced. If people want to cheat why not let them?
Here are some ideas:
* You could store the data in binary format. It's not hard to hack using a hex editor but at least it requires a little bit of conscious effort and it stops most regular users.
* You could use a checksum. If the file has been tampered with and the checksum has not been updated accordingly you can just discard the scores. If the user knows the algorithm it wouldn't be that difficult to get around it but most people would not be prepared to put in this kind of effort, and if they do don't you think they deserve a high score. ;)
very true the second idea sounds like the way to go,
I'm experimenting with binary files at the moment, but seem to be having an issue calculating the size when reading,
so lets suppose we want to read an int from a file then two strings like in my example,how would we first read that int (well that's probably a bad example all we would have to do is sizeof(int) as it will always be the same(on most systems)) but for reading a string,strings will differ in sizes so how do we successfully read the string from the file,how do we get the size of the string we want to read?
the program also seems to crash when reading from the binary file I'm not sure why also the cast I am doing is redundant(in BinaryRead() ) right? as age str is already a char*
The solution that many flash games with online high scores was not to solve the problem, you will still see people with hacked high scores in those games, because at the end, there is no way to stop hackers in single player games.
And most cheaters are not smart enough to look inside your appdata or windows registry to figure it out.
And for single player games, cheaters are the last people you should worry about (unless you have an online highscore board, but there's nothing you can do easily about that).
If you want the user to be able to transfer the file between different computers on different systems you can use fixed size integer types such as std::uint32_t in <cstdint>.
Probably the simplest way to write a string to a binary file is by just writing all the characters including the null characters and then use getline to read it back.
just wondering why my program crashes though isn't the data (int) I saved to the file 32 bits? now when I try to read it back from the file to my program the program crashes :/
I'm not sure what ageStr is for. You want to read the data into the age variable.
that worked perfect,but how would I then cast it back to an int? as casted the age to a char* with in.read(reinterpret_cast<char*>(&age), sizeof(age)); or the cstyle cast in.read((char*)&age,sizeof(int));
cast the int to a char pointer?? for some reason I can still very much do arithmetic on it so I'm guessing it is still an int,but didn't the lines above not cast it??
Casting does not change the type of existing variables. &age gives us an int* and it is this pointer that is cast to a char*. The read function then reads the specified number of bytes from file and writes them to the bytes where the pointer points to.
Note that the read function does not know anything about the original type of the thing that it writes to. It just treats it as a sequence of bytes.
Casting does not change the type of existing variables. &age gives us an int* and it is this pointer that is cast to a char*. The read function then reads the specified number of bytes from file and writes them to the bytes where the pointer points to.
thanks Peter,that kind of makes sense but why a char* why not an int pointer? or maybe more fittingly a void* ?
Functions such as memcpy uses void* which is much more convenient to use because they don't require explicit casting. I don't know why char* is used here but I suspect it goes back a long time to C where most pointer conversions happened implicitly so it probably didn't matter much which one was used. If these functions were added today they would probably have used std::byte*.