I have a text file that contains high scores for the game in this format:
Name Score Name Score Name Score
With a file sorted in descending order by result.
I want to insert a new name and its corresponding rating in the right place in the file so that it remains ordered correctly. Any suggestions on how to do this would be greatly appreciated!
For example, with the following file:
Edward 100 David 90 Sarah 80 Alice 60
And I want to add.
Name = Jodi Score = 70
To the file, so the new file reads:
Edward 100 David 90 Sarah 80 Jodi 70 Alice 60
thanks
Currently I have the following code:
string playerName = pPlayer->GetName(); int playerScore = pPlayer->GetScore(); std::ofstream score("scores.txt", std::ios_base::app | std::ios_base::out); score << "\n" << playerName << " " << playerScore;
Which only adds the name to the end of the file. I believed that I read the entire file, ordering it, and then turning the old file over. But I do not want to do this, because it can take a long time if the file becomes large.
c ++ string file-io
stell1315
source share