C++中ifstreamofstreamfstream浅谈

xiaoxiao2021-02-27  373

C++ 通过以下几个类支持文件的输入输出:

ofstream: 写操作(输出)的文件类 (由ostream引申而来) ifstream: 读操作(输入)的文件类(由istream引申而来) fstream: 可同时读写操作的文件类 (由iostream引申而来)

使用ifstream类从文件输入 使用ofstream类输出到文件 使用fstream类进行文件输入和输出 使用时加头文件#include<fstream>

打开文件(Open a file)

void open (const char * filename, openmode mode);

mode为以下标志的组合: 文件模式常量为ios_base::XXX或者ios::XXX

例子:

#include <fstream> //utifstream.cpp ... ofstream conf; conf.open("1.txt", ios::out|ios::app|ios::trunc); ...

关于三者详细的叙述读者请参看《C++ primer plus》第17章或者 http://www.cnblogs.com/azraelly/archive/2012/04/14/2446914.html

转载请注明原文地址: https://www.6miu.com/read-4657.html

最新回复(0)