【问题标题】:File write, without deleting old lines?文件写入,不删除旧行?
【发布时间】:2016-03-02 01:39:42
【问题描述】:

我目前有这个要写入我程序中的文件。我已经准备好将所有变量放入文件中,我只是有一个问题。

void fileWrite(int score)
{
    ofstream file;
    file.open("MathGen.txt");
    file << "The score was: " << score;
    file.close();
}

因此,当它运行时,它会创建文件并完美地写入乐谱。但是,如果我重新运行程序并获得新分数,它将覆盖旧分数。有没有办法阻止这种情况发生?我知道在 python 中你可以使用文件写入函数,比如“a+”。但这在这里不起作用。

【问题讨论】:

  • ios::ate 添加到标志中。

标签: c++ fwrite


【解决方案1】:

您应该在ofstream.open 中的文件上使用附加 模式:

file.open("MathGen.txt", std::ofstream::app);

有关此方法的更多信息是here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    • 2021-12-01
    • 2016-01-30
    • 1970-01-01
    • 2020-10-06
    • 2013-05-22
    相关资源
    最近更新 更多