Archived

This topic is now archived and is closed to further replies.

High scores

This topic is 5135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is probably easy to do, but what I want my game to do is when the program is run, it loads up the highscore from a previous file, and if a new highscore is made it saves it. So it always has a high score saved. How would I do this?

Share this post


Link to post
Share on other sites
the easiest way would just be to, as you say, create a file to read from and save to. something like this:

int GetHighScore(void){
int score;
FILE *pfile = fopen("score.dat","r");
fscanf(pfile,"%d",&score);
fclose(pfile);
return score;
}

void SaveScore(int score){
FILE *pfile = fopen("score.dat","w");
fprintf(pfile,"%d",score);
fclose(pfile);
}

Sorry if there are any syntax errors or anything like that... This is off the top of my head. All it does is, when saving, create a file and save a string containing the score to it. When loading, it will open that same file and read in that string, and put it in an integer value. Be careful with that GetHighScore function though: it will blow up on you if it tries to open a file that doesn''t actually exist. You might want to add something to check for the file first.

-Arek the Absolute

Share this post


Link to post
Share on other sites