在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,
一,c++ 文件流的结构:
1,几个文件流类名称:fstream,ifstream,ofstream,iofstream
2,之间的关系:
ifstream(input file stream)和ofstream(outpu file stream),ifstream默认以输入方式打开文件,而ofstream默认以输出方式打开文件。
ifstream file2("c://pdos.def");//以输入方式打开文件,输入方式:读文档
ofstream file3("c://x.123");//以输出方式打开文件 ,输出方式:写文档,向文档输出内容
iostream是对屏幕上输入输出
--------------------------------------
二,构造函数:(ofstream与 ifstream的构造函数与fstream的相同)
1,fstream fout("parts.txt");
2,fstream logfile("database.dat", ios::in | ios::out);
定义了下列打开模式和文件属性:
ios::app // 从后面添加
ios::ate // 打开并找到文件尾
ios::binary // 二进制模式 I/O (与文本模式相对)
ios::in // 只读打开
ios::out // 写打开
ios::trunc // 将文件截为 0 长度
你可以用位域操作符 OR 组合这些标志:
ofstream logfile("login.dat", ios::binary | ios::app); //向现有文件中添加二进制数据
std::fstream test("hahahah",ios::in | ios::out | ios::trunc); //读、写、将文件设定为空
3,std::ifstream readfile();
--------------------------------------
二,打开文件的几种方式:
1,使用上面的构造函数:
std::ofstream logfile("log.dat