29

I'm trying to create a basic highscore system for a project I'm working on.

The problem I'm having is, although I write the names into my main they just overwrite the previous.

Currently I have this:

void ManagePoint::saveScore(string Name, int Score)
{

    ofstream newFile("scorefile.txt");

    if(newFile.is_open())   
    {
        newFile << Name << " " << Score;            
    }
    else 
    {
        //You're in trouble now Mr!
    }


    newFile.close();

}

and for testing I'm adding them like so:

runner->saveScore("Robert", 34322);

runner->saveScore("Paul", 526);

runner->saveScore("Maxim", 34322);

On load display all that will appear is Maxim's score, how can I loop through and save them all, or append all or something?

JShorthouse
  • 706
  • 6
  • 23
Springfox
  • 1,139
  • 2
  • 10
  • 11
  • Have a look at some options via [documentation](http://en.cppreference.com/w/cpp/io/basic_ofstream/basic_ofstream). – chris Feb 24 '13 at 20:42

1 Answers1

44

You need to open the file with the append mode:

ofstream newFile("scorefile.txt", std::ios_base::app);

There are various other modes too.

Joseph Mansfield
  • 100,738
  • 18
  • 225
  • 303
  • 3
    Ah that got it, they've all saved now. Still have a problem with my loading and displaying though. Baby steps. :) Thanks. – Springfox Feb 24 '13 at 20:47