使用fstream附加到文件而不是覆盖 [英] Append to a File with fstream Instead of Overwriting

查看:840
本文介绍了使用fstream附加到文件而不是覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我正在从事的项目创建一个基本的高分系统.

I'm trying to create a basic highscore system for a project I'm working on.

我遇到的问题是,尽管我将名称写到了主名称中,但它们却覆盖了以前的名称.

The problem I'm having is, although I write the names into my main they just overwrite the previous.

目前我有这个:

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();

}

为了进行测试,我将其添加为:

and for testing I'm adding them like so:

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

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

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

在加载显示时,将显示的只是Maxim的得分,我该如何循环浏览并保存所有得分,或追加全部或某些内容?

On load display all that will appear is Maxim's score, how can I loop through and save them all, or append all or something?

推荐答案

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

You need to open the file with the append mode:

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

还有其他各种模式.

这篇关于使用fstream附加到文件而不是覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆