1、文件流ios
ofstream,由ostream派生而来,用于写文件
ifstream,由istream派生而来, 用于读文件
fstream,由iostream派生而来,用于读写文件
编程
2、打开文件app
说明了流对象以后,可以使用函数open()打开文件。文件的打开便是在流与文件之间创建一个链接
函数原型
函数
void open(const char * filename, int mode = ios::out,int prot = _SH_DENYNO);spa
参数
filename:文件的名称,能够包含(绝对和相对)路径
mode:文件打开模式
prot:保护模式
指针
(一)、文件打开模式对象
打开方式 描述
进程
ios::in 打开一个供读取的文件(ifstream流的默认值)ci
ios::out 打开一个供写入的文件(ofstream流的默认值) ios::app 在写以前找到文件尾 ios::ate 打开文件后当即将文件定位在文件尾 ios::trunc 废弃当前文件内容 ios::nocreate(已再也不支持) 若是要打开的文件并不存在,那么以此参数调用open()函数将没法进行 ios::noreplace (已再也不支持) 若是要打开的文件已存在,试图用open()函数打开时将返回一个错误。 ios::binary 以二进制的形式打开一个文件,默认为文本文件
原型
(二)、保护模式
#define _SH_DENYRW 0x10 /* deny read/write mode */拒绝对文件进行读写
#define _SH_DENYWR 0x20 /* deny write mode */拒绝写入文件
#define _SH_DENYRD 0x30 /* deny read mode */拒绝文件的读取权限
#define _SH_DENYNO 0x40 /* deny none mode */读取和写入许可
#define _SH_SECURE 0x80 /* secure mode */共享读取,独占写入
注意:假设A进程以_SH_DENYRW 打开,那么是B进程不能再对文件进行读写。
(三)、文件打开模式的有效组合
上述全部的打开模式组合还能够添加ate模式。对这些模式添加ate模只会改变文件打开时的初始定位,在第一次读或
写以前,将文件定位于文件末尾处。
(四)、文件打开的几点说明
一、文件打开也能够经过构造函数打开,例如:
ofstream fout(“out.txt“,ios::out);
二、文件的打开方式能够为上述的一个枚举常量,也能够为多个枚举常量构成的按位或表达式。
三、使用open成员函数打开一个文件时,若由字符指针参数所指定的文件不存在,则创建该文件。(out)
四、当打开方式中不含有ios::ate或ios::app选项时,则文件指针被自动移到文件的开始位置,即字节地址为0的位置。
五、从效果上看ofstream指定out模式等同于指定了out和trunc模式
六、默认状况下,fstream对象以in和out模式同时打开。
七、当文件同时以in和out打开时不会清空
八、若是只使用out模式,而不指定in模式,则文件会清空现有数据。
九、若是同时指定了out与app,不会清空
十、若是打开文件时指定了trunc模式,则不管是否同时指定了in模式,文件一样会被清空
3、流状态
对应于这个标志字各状态位,ios类还提供了如下成员函数来检测或设置流的状态:
bool rdstate(); //返回流的当前状态标志字 bool eof(); //返回非0值表示到达文件尾 bool fail(); //返回非0值表示操做失败 bool bad(); //返回非0值表示出现错误 bool good(); //返回非0值表示流操做正常 bool clear(int flag=0); //将流的状态设置为flag
为提升程序的可靠性,应在程序中检测I/O流的操做是否正常。当检测到流操做出现错误时,能够经过异常处理来解决问题。
4、文件的关闭
每一个文件流类中都提供有一个关闭文件的成员函数close()
功能:当打开的文件操做结束后,就须要关闭它,使文件流与对应的物理文件断开联系,并可以保证最后输出到文件缓冲区中的内容,不管是否已满,都将当即写入到对应的物理文件中
函数原型:void close();
文件流对应的文件被关闭后,还能够利用该文件流调用open成员函数打开其余的文件,最好先clear 一下。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
#include <cassert> #include <iostream> #include <fstream> using namespace std; int main(void) { /******************************************************/ //若不存在文件,会建立文件 //ofstream fout; //fout.open("test.txt"); ofstream fout("test.txt", ios::out | ios::app); //判断流状态 //if (fout.is_open()) //{ // cout<<"succ"<<endl; //} //else // cout<<"failed"<<endl; //if (fout.good()) //{ // cout<<"succ"<<endl; //} //else // cout<<"failed"<<endl; //if (fout) //{ // cout<<"succ"<<endl; //} //else // cout<<"failed"<<endl; //if (!fout) //{ // cout<<"failed"<<endl; //} //else // cout<<"succ"<<endl; assert(fout); fout.close(); /************************************************************/ // 若文件不存在,不会建立文件 ifstream fin("test2.txt"); // assert(fin); //文件不存在,断言失败 fin.close(); /**********************************************************/ //ofstream fout1("test3.txt", ios::in | ios::out | ios::ate); //ofstream fout2("test3.txt", ios::in | ios::out | ios::ate); //ofstream fout1("test3.txt", ios::in | ios::out | ios::app); //ofstream fout2("test3.txt", ios::in | ios::out | ios::app); // app 和 ate 能够共存,以app为准 ofstream fout1("test3.txt", ios::in | ios::out | ios::app | ios::ate); ofstream fout2("test3.txt", ios::in | ios::out | ios::app | ios::ate); assert(fout1); assert(fout2); fout1 << "X"; fout2 << "Y"; //Y 输出在 X 的后面 fout1.close(); fout2.close(); /****************************************************************/ // app 与 trunc 不能共存,流状态为fail ofstream fout3("test.txt", ios::out | ios::app | ios::trunc); if (fout3.good()) { cout << "good" << endl; } if (fout3.bad()) { cout << "bad" << endl; } if (fout3.fail()) { cout << "fail" << endl; } if (fout3.eof()) { cout << "eof" << endl; } fout3.clear(); fout3.open("test.txt"); // clear以后可以从新open if (fout3) { cout << "open succ" << endl; } else cout << "open failed" << endl; fout3.close(); return 0; } |
参考:
C++ primer 第四版 Effective C++ 3rd C++编程规范