c++ - 如何在 C++ 中追加到文件?
问题描述
我想通过几个单独的调用将一些数据打印到文件中。我注意到写入的默认行为会覆盖以前写入的数据。
#include <iostream>
#include <fstream>
using namespace std;
void hehehaha (){
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
}
int main () {
for(int i=0; i <3 ; i++){
hehehaha();}
return 0;
}
这段代码只写了一行 Writing this to a file.
,但我想要的是以下内容:
Writing this to a file.
Writing this to a file.
Writing this to a file.
解决方案
app
改为以模式打开文件myfile.open("example.txt", std::ios_base::app);
推荐阅读
- machine-learning - 如何在单个 png 文件上测试此模型
- azure - Azure 函数消费计划队列随机停止触发
- c# - 为什么 IList 类型的属性不能在接口中实现 IEnumerable 类型的属性?
- ecmascript-6 - 在 ES6 中展开对象数组
- azure - Azure 逻辑应用 - 失败的警报通知
- python - 使用用户定义的参数通过 livy 提交 pyspark 作业
- go - 从 Go 中的字符串中提取匹配模式
- java - 如何使用hibernate引用表中的现有数据
- guice - 如何将非 Guice 实例注入 Guice 模块
- javascript - 防止 HTML 文件输入在使用 Android 的本机文件选择器时选择 Google Drive 中的文件