FILE *dataScore;
dataScore = fopen(fileName.dat, "w");
fprintf(dataScore,"%s:%d\n",currentUser,score);
fclose(dataScore);
文件在打印到文件的行上崩溃。我相信这是由于用户名,但我可能是错的。提前致谢。设置currentUser为02heasam,得分为20。
最佳答案
看起来很疯狂......
试试这个方法:
int score=20;
int main(void){
char* currentUser = "02heasam";
FILE *dataScore;
dataScore = fopen("fileName.dat", "w");
fprintf(dataScore,"%s:%d\n",currentUser,score);
fclose(dataScore);
}
一些解释:
- 要用字符串填充 char 数组,您需要 strcpy 左右。这里不需要!
- 顺序可能很重要(使用前声明)
- strimng 文字“xxx”将自动以尾随的 0 字节终止 - 千万不要错过这一点!
关于C - 保存到文件崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22157733/