私が作成したゲームがあります。ハイスコアを読み込んで保存する機能を実装したい。最初はテキストファイルがないとき(私の場合highscore.txt
)、作成したらファイルに0を出力したいと思います。ロード中にある場合は、前回のゲームプレイのハイスコアをロードしたいと思います。これについてどうすればいいですか?
これが私のコードです:
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 は、すでにファイルにあったものを上書きし、空白のままにしているようです