C++中ifstream/ofstream/fstream浅谈

来源:互联网 发布:windows获取时间戳 编辑:程序博客网 时间:2024/06/03 18:43

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

1 0
原创粉丝点击