Ok! so im making a simple highscore type thing. the person plays the game and receives a score within the game. At the end of the game i have it currently comparing the score received with the score already in place within the highscore text file. the problem im having is getting the recorded score in the file to change with the new highscore received while still meeting the conditions of the if statement. i can set cScore1 = cScore but then the condition won't be met i'm out of ides please help here's a code portion

ofstream Highscore ("Highscore.txt");
 
 if(cScore > cScore1)
 {
	 gotoxy(18,13); cout << "New Highscore! " << endl;
 
 Highscore << name << "|" << cScore1 << endl;
 Highscore.close();
}

 ifstream Highscores ("Highscore.txt");
  if (Highscores.is_open())
  {
    while ( Highscores.good() )
    {
      getline (Highscores,line);
      cout << line << endl;
    }
    Highscores.close();
  }

sorry if this is confusing but its hard to explain. i have a line in a text file that says the players name and then the score for the player. i want it to replace that text with the name of the player that gets a higher score and replace the score with the higher score. if the next players score is lower than that of the text file then it doesn't change. i cant explain it any better sorry.

It's not easy to replace a line of text in a file, because if the replacement line is a different size than the original, you have to change the positions of the remaining characters in the file.

Unless you want to get into databases, the easiest most reliable way to change the contents of a file is to copy the entire file into a new file with a different name. Once that step is complete, you can delete the original file and rename the new file to have the same name as the original.

Obviously this technique does not scale well to very large files, but if you are going to deal in this way with very large files, you will have other problems to solve as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.