避免在写入文件时覆盖现有文件的内容 [英] Avoid contents of an existing file to be overwritten when writing to a file

查看:207
本文介绍了避免在写入文件时覆盖现有文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个将高分转换成.txt文件的游戏.我的问题是:当我发表这样的声明时:

I am trying to make a game that implements high scores into a .txt file. The question I have is this : when I make a statement such as:

ofstream fout("filename.txt");

这是创建具有该名称的文件,还是只是查找具有该名称的文件?

Does this create a file with that name, or just look for a file with that name?

问题是,每当我重新启动该程序并做出以下声明时:

The thing is that whenever I start the program anew and make the following statement:

fout << score << endl << player; 

它覆盖了我以前的分数!

it overwrites my previous scores!

我有什么办法做到这一点,以便在我写入文件时新的乐谱不会覆盖旧的乐谱?

Is there any way for me to make it so that the new scores don't overwrite the old ones when I write to the file?

推荐答案

std::ofstream默认情况下会创建一个新文件.您必须使用附加参数创建文件.

std::ofstream creates a new file by default. You have to create the file with the append parameter.

ofstream fout("filename.txt", ios::app); 

这篇关于避免在写入文件时覆盖现有文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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