【问题标题】:C - Saving to file crashesC - 保存到文件崩溃
【发布时间】:2014-03-03 21:22:35
【问题描述】:
    FILE *dataScore;
    dataScore = fopen(fileName.dat, "w");
    fprintf(dataScore,"%s:%d\n",currentUser,score);
    fclose(dataScore);

文件在打印到文件的行上崩溃。我相信这是由于用户名,但我可能是错的。提前致谢。设置currentUser为02heasam,得分为20。

【问题讨论】:

  • currentUser = 02heasam; 不是完全有效的 C 开头...
  • @Marc B 我知道,例如它就在那里。为了清楚起见,我只是把它放在代码之外。

标签: c file crash save


【解决方案1】:

看起来很疯狂...

试试这个方法:

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 左右。这里不需要!
  • 顺序可能很重要(使用前声明)
  • string 文字“xxx”将自动以尾随 0 字节结尾 - 千万不要忘记这一点!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多