请教怎么把文件句柄作为参数传递到函数中
请问如何把文件句柄作为参数传递到函数中
我想对一个文件在函数体和主函数中进行写操作,所以需要把文件句柄而不是文件名传递到函数中。(因为函数要被反复调用,如果每次都打开就消耗太大了),请问如何实现。我试着用下面的方式进行,但是失败了。报错:can not access private member.谢谢!
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <io.h>
using namespace std;
void write(fstream out)
{
out<<"successful"<<endl;
}
int main(int argc, char **argv)
{
fstream out;
out.open("output.txt",ios::app);
write(out);
out.close();
}
------解决方案--------------------
我想对一个文件在函数体和主函数中进行写操作,所以需要把文件句柄而不是文件名传递到函数中。(因为函数要被反复调用,如果每次都打开就消耗太大了),请问如何实现。我试着用下面的方式进行,但是失败了。报错:can not access private member.谢谢!
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <io.h>
using namespace std;
void write(fstream out)
{
out<<"successful"<<endl;
}
int main(int argc, char **argv)
{
fstream out;
out.open("output.txt",ios::app);
write(out);
out.close();
}
------解决方案--------------------
- C/C++ code
void write(fstream& out)//这样 { out < <"successful" < <endl; }