第一步:包含头文件#include <fstream>。
#include <iostream>
#include <fstream>
using namespace std;
第二步:以覆盖的方式保存文件。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream out("out.txt");
if (out.is_open())
{
out << "This is a line.\n";
out << "This is another line.\n";
out.close();
}
system("pause");
return 0;
}
第三步:以追加的方式保存文件 ios::app。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream out("out.txt", ios::app);
if (out.is_open())
{
out << "This is a line.\n";
out << "This is another line.\n";
out.close();
}
system("pause");
return 0;
}