输入输出流采用引用调用可以事先不用传文件名
#include<fstream>
using namespace std;
void print_row(ofstream& out, char c, int n);
int main()
{
ofstream outfile;
outfile.open("out2.txt");
print_row(outfile, 'c', 10);
outfile.close();
return 0;
}
void print_row(ofstream& out, char c, int n)
{
for(int i=0;i<n;i++)
out<<c<<endl;
}
C++ 文件输出示例
本文展示了一个使用 C++ 编写的简单程序示例,该程序利用 fstream 类中的 ofstream 对象来创建并写入一个名为 out2.txt 的文本文件。通过对引用的使用,实现了在不直接传递文件名的情况下进行文件操作。
474





