title: Loading and Saving from a file c++ highscore function
body: <p>I have a game that I have created. I want to implement a function that loads and saves  high scores. At first when there is no text file (in my case <code>highscore.txt</code>), once created I want to print 0 to the file. If it is there on load I want it to load the highscore from the previous playing of the game. How would I go about this?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>void filewrite(){
  
      int n;
  
      FILE *FPtr;
      FPtr = fopen("highscore.txt","w+");//open file
      if (FPtr == NULL){
      fprintf (FPtr, "%d" , highscore);
      rewind(FPtr);
      }
      fscanf(FPtr,"%d",&amp;n);//scan file for integer
  
  
      if (n &lt; score){
          if(highscore &lt; score ){
              if(end==true){
                  fprintf (FPtr, "%d" , score);//print the score to file if it is higher than the score already there
                  rewind(FPtr);//rewind the file, so pointer starts at begining
                  fscanf(FPtr,"%d",&amp;highscore);//scan file for int
              }
          }
      }
  
      fclose(FPtr);//close pointer to file
  
  }
  </code></pre>
  
  <p>fopen seems to overwrite what was in the file already and leaves it blank</p>