一、I/O流
头文件 <fstream>
C++中把对文件的读写操作都封装在以下类中:
ofstream 对文件的写操作,继承了ostream类的功能
ifstream 对文件的读操作,继承了istream类的功能
fstream 对文件的读写操作,继承了ofstream和ifstream类的功能
二、对文本文件的读写操作
1、创建流对象,通过流对象打开文件
a、创建流对象并用有参构造打开文件
ofstream fout(const char *filename,<openmode mode>);
b、无参构造创建流对象,再通过成员函数打开文件
void open(const char *filename);
void open(const char *filename, openmode mode);
mode: 文件的打开方式
ios::app 添加输出
ios::ate 当已打开时寻找EOF
ios::binary 以二进制模式打开文件
ios::in 为读取打开文件
ios::out 为写入打开文件
ios::trunc 覆盖存在的文件
ifstream类 默认只读方式打开文件,文件不存在则失败 r
ofstream类 默认只写方式打开问价,文件不存在则创建&#