在fstream类中,成员函数open()实现打开文件的 *** 作,从而将数据流和文件进行关联,通过ofstream,ifstream,fstream对象进行对文件的读写 *** 作
函数:open()
<span style="font-family:Times New Romanfont-size:16px">public member function
void open ( const char * filename,
ios_base::openmode mode = ios_base::in | ios_base::out )
void open(const wchar_t *_Filename,
&nbsp&nbsp &nbsp&nbsp&nbsp &nbspios_base::openmode mode= ios_base::in | ios_base::out,
&nbsp&nbsp &nbsp&nbsp&nbsp &nbspint prot = ios_base::_Openprot);
</span>
参数: filename *** 作文件名
mode 打开文件的方式
prot 打开文件的属性
很多程序中,可能会碰到ofstream out("Hello.txt"), ifstream in("..."),fstream foi("...")这样的的使用,并没有显式的去调用open()函数就进行文件的 *** 作,直接调用了其默认的打开方式,因为在stream类的构造函数中调用了open()函数,并拥有同样的构造函数,所以在这里可以直接使用流对象进行文件的 *** 作,默认方式如下:
<span style="font-family:Times New Romanfont-size:16px">ofstream out("...", ios::out)
ifstream in("...", ios::in)
fstream foi("...", ios::in|ios::out)
</span>
当使用默认方式进行对文件的 *** 作时,可以使用成员函数is_open()对文件是否打开进行验证
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)