title: C++ Inserting a line at specific point in file
body: <p>I have a text file that contains the high scores of a game in this format:</p>
  
  <pre><code>Name Score
  Name Score
  Name Score
  </code></pre>
  
  <p>With the file ordered in descending order on score.</p>
  
  <p>I want to insert a new name and its corresponding score in the right place in the file so that it remains ordered correctly. Any suggestions how to do this would be greatly appreciated!</p>
  
  <p>For Example, given the following file:</p>
  
  <pre><code>Edward 100
  David 90
  Sarah 80
  Alice 60
  </code></pre>
  
  <p>And I want to add.</p>
  
  <pre><code>Name = Jodi
  Score = 70
  </code></pre>
  
  <p>To the file, so the new file reads:</p>
  
  <pre><code>Edward 100
  David 90
  Sarah 80
  Jodi 70
  Alice 60
  </code></pre>
  
  <p>Thanks</p>
  
  <p>At the moment I have the following code:</p>
  
  <pre><code>string playerName = pPlayer-&gt;GetName();
  int playerScore = pPlayer-&gt;GetScore();
  
  std::ofstream score("scores.txt", std::ios_base::app | std::ios_base::out);
  
  score &lt;&lt; "\n" &lt;&lt; playerName &lt;&lt; " " &lt;&lt; playerScore;
  </code></pre>
  
  <p>Which just adds the name to the end of the file. I considered reading in the whole file ordering it then Over righting the old file. But I don't want to do this because it could take ages if the file got large. </p>