Gle*_*leb 1 c++ fstream ofstream
问题是如何使输出文件1在第1行,2第2行等,因为程序因为它在每次执行循环时重写文件而你只留9在输出文件中.
#include <fstream>
using namespace std;
void function (int i)
{
ofstream output("result.out");
output << i << endl;
output.close();
}
int main()
{
for (int i=1; i<10; i++)
{
function(i);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
传递std::ios::app给std::ofstream构造函数的第二个参数.即
std::ofstream output("result.out", std::ios::app);
Run Code Online (Sandbox Code Playgroud)