C - 保存到文件崩溃

标签 c file crash save

    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/

相关文章:

c - 如何解释此调试错误

git - 通过 build.gradle 任务在项目目录中创建 version.txt 文件

C编程: reading and writing binary files

iOS - 在 main.m 崩溃,没有日志,没有可能的断点

javascript - Django,Javascript : Form injected into DOM via javascript innerHtml crashes google chrome tab on submit. 在 IE 中工作

android - "cannot combine custom titles with other title features"和 "Source code does not match binarycode"

C程序概念

无法打开要追加的特定文件名

c - 像宏这样的功能是什么意思?

java - 使用 FileUtils.listfiles 读取子目录时出现问题