我想通过几次单独的调用将一些数据打印到文件中。我注意到写入的默认行为会覆盖以前写入的数据。
#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)