我已经创建了一个代码写入.txt文件的东西,并从中读取。但是如果我关闭程序并重新开始写入,它将删除旧文本并用新文本覆盖它。如何在C++中保存.txt文件?
有没有办法不覆盖现有的数据?
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void check() {
string text;
ifstream file;
file.open("test.txt");
getline(file, text);
if (text == "") {
cout << "There's no data in file" << endl;
} else {
cout << "File contains:" << endl;
cout << text << endl;
}
}
int main() {
check();
string text;
ofstream file;
file.open("test.txt");
cout << "Type some text" << endl;
getline(cin, text);
file << text;
return 0;
}
嗨,请显示您尝试过的一些代码,然后我们可以尝试帮助您 –
您所说的这个“问题”是什么?这听起来像是按照预期工作。 –
我发布了我的代码 –