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++編程規範