namespace: std ifstream – 從已有的文件讀 ofstream – 向文件寫內(nèi)容 fstream - 打開文件供讀寫
成員函數(shù)open()實(shí)現(xiàn)打開文件的操作,從而將數(shù)據(jù)流和文件進(jìn)行關(guān)聯(lián),通過ofstream,ifstream,fstream對象進(jìn)行對文件的讀寫操作
void open ( const char * filename, ios_base::openmode mode = ios_base::in | ios_base::out ); void open(const wchar_t *_Filename, ios_base::openmode mode= ios_base::in | ios_base::out, int PRot = ios_base::_Openprot);參數(shù): filename 操作文件名 mode 打開文件的方式 prot 打開文件的屬性 打開文件的方式在iOS類(所以流式I/O的基類)中定義,有如下幾種方式:
打開文件的屬性同樣在ios類中也有定義:
很多程序中,可能會碰到ofstream out(“Hello.txt”), ifstream in(“…”),fstream foi(“…”)這樣的的使用,并沒有顯式的去調(diào)用open()函數(shù)就進(jìn)行文件的操作,直接調(diào)用了其默認(rèn)的打開方式,因?yàn)樵趕tream類的構(gòu)造函數(shù)中調(diào)用了open()函數(shù),并擁有同樣的構(gòu)造函數(shù),所以在這里可以直接使用流對象進(jìn)行文件的操作;
打開文件時一定要檢查文件是否已經(jīng)打開 不操作文件一定關(guān)閉文件 getline(infile,s)將文件寫入string中
#include<iostream>#include<fstream>#include<string>using namespace std;int main(){ string filename = "1.txt"; fstream infile(filename, ios::in); if (!infile) { throw runtime_error("file cannot open"); return -1; } else { string s; while (!infile.eof()) { getline(infile, s); cout << s << endl; } } infile.close(); return 0;}#include<iostream>#include<fstream>#include<string>using namespace std;int main(){ string filename = "1.txt"; ofstream outfile; outfile.open(filename, ios::out | ios::app); if (!outfile) { throw runtime_error("file cannot open"); } else { outfile << "111" << "222" << endl; outfile.close(); }}狀態(tài)標(biāo)志符的驗(yàn)證(Verification of state flags) 除了eof()以外,還有一些驗(yàn)證流的狀態(tài)的成員函數(shù)(所有都返回bool型返回值): bad() 如果在讀寫過程中出錯,返回 true 。例如:當(dāng)我們要對一個不是打開為寫狀態(tài)的文件進(jìn)行寫入時,或者我們要寫入的設(shè)備沒有剩余空間的時候。 fail() 除了與bad() 同樣的情況下會返回 true 以外,加上格式錯誤時也返回true ,例如當(dāng)想要讀入一個整數(shù),而獲得了一個字母的時候。 eof() 如果讀文件到達(dá)文件末尾,返回true。 good() 這是最通用的:如果調(diào)用以上任何一個函數(shù)返回true 的話,此函數(shù)返回 false 。 要想重置以上成員函數(shù)所檢查的狀態(tài)標(biāo)志,你可以使用成員函數(shù)clear(),沒有參數(shù)。
我們可以通過使用以下成員函數(shù)來讀出或配置這些指向流中讀寫位置的流指針: tellg() 和 tellp() 這兩個成員函數(shù)不用傳入?yún)?shù),返回pos_type 類型的值(根據(jù)ANSI-C++ 標(biāo)準(zhǔn)) ,就是一個整數(shù),代表當(dāng)前get 流指針的位置 (用tellg) 或 put 流指針的位置(用tellp). seekg() 和seekp() 這對函數(shù)分別用來改變流指針get 和put的位置。兩個函數(shù)都被重載為兩種不同的原型: seekg ( pos_type position ); seekp ( pos_type position ); 使用這個原型,流指針被改變?yōu)橹赶驈奈募_始計算的一個絕對位置。要求傳入的參數(shù)類型與函數(shù) tellg 和tellp 的返回值類型相同。 seekg ( off_type offset, seekdir direction ); seekp ( off_type offset, seekdir direction ); 使用這個原型可以指定由參數(shù)direction決定的一個具體的指針開始計算的一個位移(offset)。它可以是: 
使用這個原型可以指定由參數(shù)direction決定的一個具體的指針開始計算的一個位移(offset)。它可以是:
流指針 get 和 put 的值對文本文件(text file)和二進(jìn)制文件(binary file)的計算方法都是不同的,因?yàn)槲谋灸J降奈募心承┨厥庾址赡鼙恍薷摹S捎谶@個原因,建議對以文本文件模式打開的文件總是使用seekg 和 seekp的第一種原型,而且不要對tellg 或 tellp 的返回值進(jìn)行修改。對二進(jìn)制文件,你可以任意使用這些函數(shù),應(yīng)該不會有任何意外的行為產(chǎn)生。
4.二進(jìn)制文件 在二進(jìn)制文件中,使用<< 和>>,以及函數(shù)(如getline)來操作符輸入和輸出數(shù)據(jù),沒有什么實(shí)際意義,雖然它們是符合語法的。 文件流包括兩個為順序讀寫數(shù)據(jù)特殊設(shè)計的成員函數(shù):write 和 read。第一個函數(shù) (write) 是ostream 的一個成員函數(shù),都是被ofstream所繼承。而read 是istream 的一個成員函數(shù),被ifstream 所繼承。類 fstream 的對象同時擁有這兩個函數(shù)。它們的原型是: write ( char * buffer, streamsize size ); read ( char * buffer, streamsize size ); 這里 buffer 是一塊內(nèi)存的地址,用來存儲或讀出數(shù)據(jù)。參數(shù)size 是一個整數(shù)值,表示要從緩存(buffer)中讀出或?qū)懭氲淖址麛?shù)。
#include<iostream>#include<fstream>using namespace std;int main(){ char *filename = "1.txt"; char *buffer; long size,m; ifstream infile(filename,ios::in | ios::binary | ios::ate); size = infile.tellg(); buffer = new char[size]; infile.seekg(0, ios::beg); infile.read(buffer, size); infile.close(); cout << buffer << endl; cout << "The complete file is in a buffer" << endl; delete[] buffer; return 0;}新聞熱點(diǎn)
疑難解答
圖片精選