【问题标题】:Append to a File with fstream Instead of Overwriting使用 fstream 附加到文件而不是覆盖
【发布时间】:2013-02-09 23:21:18
【问题描述】:

我正在尝试为我正在进行的项目创建一个基本的高分系统。

我遇到的问题是,虽然我将名称写入我的主目录,但它们只是覆盖了以前的名称。

目前我有这个:

void ManagePoint::saveScore(string Name, int Score)
{

    ofstream newFile("scorefile.txt");

    if(newFile.is_open())   
    {
        newFile << Name << " " << Score;            
    }
    else 
    {
        //You're in trouble now Mr!
    }


    newFile.close();

}

为了测试,我像这样添加它们:

runner->saveScore("Robert", 34322);

runner->saveScore("Paul", 526);

runner->saveScore("Maxim", 34322);

在加载显示时,所有出现的都是Maxim的分数,我怎样才能循环并保存它们,或者全部附加或什么?

【问题讨论】:

标签: c++ file overwrite


【解决方案1】:

您需要使用附加模式打开文件:

ofstream newFile("scorefile.txt", std::ios_base::app);

还有various other modes

【讨论】:

  • 啊,知道了,现在都保存了。我的加载和显示仍然有问题。婴儿步。 :) 谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
  • 1970-01-01
  • 2015-04-29
  • 2018-09-15
  • 1970-01-01
  • 2013-11-10
  • 1970-01-01
相关资源
最近更新 更多