• 11
• 9
• 10
• 9
• 10

# Hi Scores Implementation using fstream

This topic is 4708 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a file for hi scores in the following format: Name1 100000 Name2 80000 Name3 70000 ... Name10 1000 I'm using fstream, but do not know all the functions associated with it. Basically, when a new score is registered, I want to be able to insert it (with the associated Name) in the appropriate spot and remove the last score, so only 10 names total is listed at all times. How would I do this? I'm thinking I would need to create a temp file, copy all scores >= new score to the temp file, then copy all scores < new score, leaving out the smallest high score. Then clear the original high scores file and copy the temp file to the high scores file. Is it necessary to do all this or can I simply insert the new score into the high scores file? What functions do I need to use? Finally, is there a website you recommend that details fstream and all its functions? Thanks

##### Share on other sites
What I would do is have a highscore class that, at the end of every game, would test to see if you score is higher than the lowest highscore and replace it. Before you do this, obviously, you would parse in the last set of highscores to test against.

Here is a reference on fstream. I would like to note that it was the first hit on google.

##### Share on other sites
This seems all good; however what happens when you quit the game? Every time you restart, the data from the previous game won't be saved, which is why I created a file to store this info. I'll look at your link for fstream.

##### Share on other sites
Whenever player quits the game (actually just before it) you check your score file as Mercury had explained you.
Or do you mean an unexpected quit? Not invoked by player? Then i would check against the score file every loop of your main() function, or after every action a player did. Then save (if it's bigger than the lowest) the score to the file and thats it.
Hope this helps a little ;)
Y.

##### Share on other sites
I just released Blastoids which features an encrypted highscore list using TinyXML and a extremely modified version of Tiny Encryption Algorithm. The source is available through the website. Good luck sorting through it all!

##### Share on other sites
To update, don't bother with a second file. Instead:

- Read the existing file into some data structure in memory, and close the file.
- Update the in-memory data with the new structure.
- Reopen the existing file for (over)writing, and dump the updated value.

If changes to the data are frequent, it may make sense to keep the data structure around permanently in the program, and just periodically update the file (or even just once at program exit). There are a few tradeoffs here (generally speed vs. robustness - I/O is slow compared to basically everything else, and you can't do anything about it - except to time it to happen when it's not so important).

For example:

typedef std::pair<std::string, int> scoreEntry;struct scoreComparer {  bool operator() (const scoreEntry& a, const scoreEntry& b) const {    return a.second < b.second;  }}std::vector<scoreEntry> highscores;// To insert a score, perhaps inefficiently but very easily (and the efficiency// really won't matter here):highscores.push_back(std::make_pair(playerName, playerScore));std::sort(highscores.begin(), highscores.end(), scoreComparer());highscores.pop_back();

As for fstream docs, I usually use this.