小编ahm*_*igh的帖子

如何在 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;
}
Run Code Online (Sandbox Code Playgroud)

这段代码只写了一行 Writing this to a file.,但我想要的是以下内容:

Writing this to a file.
Writing this to a file.
Writing this to a file.
Run Code Online (Sandbox Code Playgroud)

c++ c++11

4
推荐指数
1
解决办法
9743
查看次数

标签 统计

c++ ×1

c++11 ×1