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 highscore.txt), 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?
Here is my 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",&n);//scan file for integer
if (n < score){
if(highscore < 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",&highscore);//scan file for int
}
}
}
fclose(FPtr);//close pointer to file
}
fopen seems to overwrite what was in the file already and leaves it blank
The steps you should take are: