目錄(?)[-] 狀態標誌符的驗證Verification of state flags 獲得和設定流指標get and put stream pointers 二進位檔案 緩衝和同步Buffers and Synchronization
在看C++編程思想中,每個練習基本都是使用ofstream,ifstream,fstream,以前粗略知道其用法和含義,在看了幾位大牛的博文後,進行整理和總結:
這裡主要是討論fstream的內容:
[java] view plain copy print ? #include <fstream> ofstream //檔案寫操作 記憶體寫入存放裝置 ifstream //檔案讀操作,存放裝置讀區到記憶體中 fstream //讀寫操作,對開啟的檔案可進行讀寫操作
#include <fstream>ofstream //檔案寫操作 記憶體寫入存放裝置 ifstream //檔案讀操作,存放裝置讀區到記憶體中fstream //讀寫操作,對開啟的檔案可進行讀寫操作
1.開啟檔案
在fstream類中,成員函數open()實現開啟檔案的操作,從而將資料流和檔案進行關聯,通過ofstream,ifstream,fstream對象進行對檔案的讀寫操作
函數:open()
[cpp] view plain copy print ? <SPAN style="FONT-FAMILY: Times New Roman; FONT-SIZE: 16px"> public member function 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); </SPAN>
public member functionvoid 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);
參數: filename 操作檔案名稱
mode 開啟檔案的方式
prot 開啟檔案的屬性 //基本很少用到,在查看資料時,發現有兩種方式
開啟檔案的方式在ios類(所以流式I/O的基類)中定義,有如下幾種方式:
| ios::in |
為輸入(讀)而開啟檔案 |
| ios::out |
為輸出(寫)而開啟檔案 |
| ios::ate |
初始位置:檔案尾 |
| ios::app |
所有輸出附加在檔案末尾 |
| ios::trunc |
如果檔案已存在則先刪除該檔案 |
| ios::binary |
二進位方式 |
這些方式是能夠進行組合使用的,以“或”運算(“|”)的方式:例如
[cpp] view plain copy print ? ofstream out; out.open("Hello.txt", ios::in|ios::out|ios::binary) //根據自己需要進行適當的選取
ofstream out;out.open("Hello.txt", ios::in|ios::out|ios::binary) //根據自己需要進行適當的選取開啟檔案的屬性同樣在ios類中也有定義:
| 0 |
普通檔案,開啟操作 |
| 1 |
唯讀檔案 |
| 2 |
隱含檔案 |
| 4 |
系統檔案 |
對於檔案的屬性也可以使用“或”運算和“+”進行組合使用,這裡就不做說明了。
很多程式中,可能會碰到ofstream out("Hello.txt"), ifstream in("..."),fstream foi("...")這樣的的使用,並沒有顯式的去調用open()函數就進行檔案的操作,直接調用了其預設的開啟檔案,因為在stream類的建構函式中調用了open()函數,並擁有同樣的建構函式,所以在這裡可以直接使用流對象進行檔案的操作,預設如下:
[cpp] view plain copy print ? <SPAN style="FONT-FAMILY: Times New Roman; FONT-SIZE: 16px"> ofstream out("...", ios::out); ifstream in("...", ios::in); fstream foi("...", ios::in|ios::out); </SPAN>
ofstream out("...", ios::out);ifstream in("...", ios::in);fstream foi("...", ios::in|ios::out);當使用預設進行對檔案的操作時,你可以使用成員函數is_open()對檔案是否開啟進行驗證
2.關閉檔案
當檔案讀寫操作完成之後,我們必須將檔案關閉以使檔案重新變為可訪問的。成員函數close(),它負責將緩衝中的資料排放出來並關閉檔案。這個函數一旦被調用,原先的流對象就可以被用來開啟其它的檔案了,這個檔案也就可以重新被其它的進程所訪問了。為防止流對象被銷毀時還聯絡著開啟的檔案,解構函式將會自動調用關閉函數close。
3.文字檔的讀寫
類ofstream, ifstream 和fstream 是分別從ostream, istream 和iostream 中引申而來的。這就是為什麼 fstream 的對象可以使用其父類的成員來訪問資料。
一般來說,我們將使用這些類與同控制台(console)互動同樣的成員函數(cin 和 cout)來進行輸入輸出。如下面的例題所示,我們使用重載的插入操作符<<:
[cpp] view plain copy print ? // writing on a text file #include <fiostream.h> int main () { ofstream out("out.txt"); if (out.is_open()) { out << "This is a line.\n"; out << "This is another line.\n"; out.close(); } return 0; } //結果: 在out.txt中寫入: This is a line. This is another line
// writing on a text file #include <fiostream.h> int main () { ofstream out("out.txt"); if (out.is_open()) { out << "This is a line.\n"; out << "This is another line.\n"; out.close(); } return 0; } //結果: 在out.txt中寫入: This is a line. This is another line
從檔案中讀入資料也可以用與 cin>>的使用同樣的方法:
[cpp] view plain copy print ? // reading a text file #include <iostream.h> #include <fstream.h> #include <stdlib.h> int main () { char buffer[256]; ifstream in("test.txt"); if (! in.is_open()) { cout << "Error opening file"; exit (1); } while (!in.eof() ) { in.getline (buffer,100); cout << buffer << endl; } return 0; } //結果 在螢幕上輸出 This is a line. This is another line
// reading a text file #include <iostream.h> #include <fstream.h> #include <stdlib.h> int main () { char buffer[256]; ifstream in("test.txt"); if (! in.is_open()) { cout << "Error opening file"; exit (1); } while (!in.eof() ) { in.getline (buffer,100); cout << buffer << endl; } return 0; } //結果 在螢幕上輸出 This is a line. This is another line
上面的例子讀入一個文字檔的內容,然後將它列印到螢幕上。注意我們使用了一個新的成員函數叫做eof ,它是ifstream 從類 ios 中繼承過來的,當到達檔案末尾時返回true 。 狀態標誌符的驗證(Verification of state flags)
除了eof()以外,還有一些驗證流的狀態的成員函數(所有都返回bool型傳回值): bad()
如果在讀寫過程中出錯,返回 true 。例如:當我們要對一個不是開啟為寫狀態的檔案進行寫入時,或者我們要寫入的裝置沒有剩餘空間的時候。 fail()
除了與bad() 同樣的情況下會返回 true 以外,加上格式錯誤時也返回true ,例如當想要讀入一個整數,而獲得了一個字母的時候。 eof()
如果讀檔案到達檔案末尾,返回true。 good()
這是最通用的:如果調用以上任何一個函數返回true 的話,此函數返回 false 。
要想重設以上成員函數所檢查的狀態標誌,你可以使用成員函數clear(),沒有參數。
獲得和設定流指標(get and put stream pointers)
所有輸入/輸出流對象(i/o streams objects)都有至少一個流指標: ifstream, 類似istream, 有一個被稱為get pointer的指標,指向下一個將被讀取的元素。 ofstream, 類似 ostream, 有一個指標 put pointer ,指向寫入下一個元素的位置。 fstream, 類似 iostream, 同時繼承了get 和 put
我們可以通過使用以下成員函數來讀出或配置這些指向流中讀寫位置的流指標: tellg() 和 tellp()
這兩個成員函數不用傳入參數,返回pos_type 類型的值(根據ANSI-C++ 標準) ,就是一個整數,代表當前get 流指標的位置 (用tellg) 或 put 流指標的位置(用tellp). seekg() 和seekp()
這對函數分別用來改變流指標get 和put的位置。兩個函數都被重載為兩種不同的原型: seekg ( pos_type position );
seekp ( pos_type position );
使用這個原型,流指標被改變為指向從檔案開始計算的一個絕對位置。要求傳入的參數類型與函數 tellg 和tellp 的傳回值類型相同。 seekg ( off_type offset, seekdir direction );
seekp ( off_type offset, seekdir direction );
使用這個原型可以指定由參數direction決定的一個具體的指標開始計算的一個位移(offset)。它可以是:
| ios::beg |
從流開始位置計算的位移 |
| ios::cur |
從流指標當前位置開始計算的位移 |
| ios::end |
從流末尾處開始計算的位移 |
流指標 get 和 put 的值對文字檔(text file)和二進位檔案(binary file)的計算方法都是不同的,因為文字模式的檔案中某些特殊字元可能被修改。由於這個原因,建議對以文字檔模式開啟的檔案總是使用seekg 和 seekp的第一種原型,而且不要對tellg 或 tellp 的傳回值進行修改。對二進位檔案,你可以任意使用這些函數,應該不會有任何意外的行為產生。
以下例子使用這些函數來獲得一個二進位檔案的大小:
[cpp] view plain copy print ? // obtaining file size #include <iostream.h> #include <fstream.h> const char * filename = "test.txt"; int main () { long l,m; ifstream in(filename, ios::in|ios::binary); l = in.tellg(); in.seekg (0, ios::end); m = in.tellg(); in.close(); cout << "size of " << filename; cout << " is " << (m-l) << " bytes.\n"; return 0; } //結果: size of example.txt is 40 bytes.
// obtaining file size #include <iostream.h> #include <fstream.h> const char * filename = "test.txt"; int main () { long l,m; ifstream in(filename, ios::in|ios::binary); l = in.tellg(); in.seekg (0, ios::end); m = in.tellg(); in.close(); cout << "size of " << filename; cout << " is " << (m-l) << " bytes.\n"; return 0; } //結果: size of example.txt is 40 bytes.
4.二進位檔案
在二進位檔案中,使用<< 和>>,以及函數(如getline)來操作符輸入和輸出資料,沒有什麼實際意義,雖然它們是符合文法的。
檔案流包括兩個為順序讀寫資料特殊設計的成員函數:write 和 read。第一個函數 (write) 是ostream 的一個成員函數,都是被ofstream所繼承。而read 是istream 的一個成員函數,被ifstream 所繼承。類 fstream 的對象同時擁有這兩個函數。它們的原型是: write ( char * buffer, streamsize size );
read ( char * buffer, streamsize size );
這裡 buffer 是一塊記憶體的地址,用來儲存或讀出資料。參數size 是一個整數值,表示要從緩衝(buffer)中讀出或寫入的字元數。
[cpp] view plain copy print ? // reading binary file #include <iostream> #include <fstream.h> const char * filename = "test.txt"; int main () { char * buffer; long size; ifstream in (filename, ios::in|ios::binary|ios::ate); size = in.tellg(); in.seekg (0, ios::beg); buffer = new char [size]; in.read (buffer, size); in.close(); cout << "the complete file is in a buffer"; delete[] buffer; return 0; } //運行結果: The complete file is in a buffer
// reading binary file #include <iostream> #include <fstream.h> const char * filename = "test.txt"; int main () { char * buffer; long size; ifstream in (filename, ios::in|ios::binary|ios::ate); size = in.tellg(); in.seekg (0, ios::beg); buffer = new char [size]; in.read (buffer, size); in.close(); cout << "the complete file is in a buffer"; delete[] buffer; return 0; } //運行結果: The complete file is in a buffer
5.緩衝和同步(Buffers and Synchronization)
當我們對檔案流進行操作的時候,它們與一個streambuf 類型的緩衝(buffer)聯絡在一起。這個緩衝(buffer)實際是一塊記憶體空間,作為流(stream)和物理檔案的媒介。例如,對於一個輸出資料流, 每次成員函數put (寫一個單個字元)被調用,這個字元不是直接被寫入該輸出資料流所對應的物理檔案中的,而是首先被插入到該流的緩衝(buffer)中。
當緩衝被排放出來(flush)時,它裡面的所有資料或者被寫入物理媒質中(如果是一個輸出資料流的話),或者簡單的被抹掉(如果是一個輸入資料流的話)。這個過程稱為同步(synchronization),它會在以下任一情況下發生: 當檔案被關閉時: 在檔案被關閉之前,所有還沒有被完全寫出或讀取的緩衝都將被同步。 當緩衝buffer 滿時:緩衝Buffers 有一定的空間限制。當緩衝滿時,它會被自動同步。 控制符明確指明:當遇到流中某些特定的控制符時,同步會發生。這些控制符包括:flush 和endl。 明確調用函數sync(): 調用成員函數sync() (無參數)可以引發立即同步。這個函數返回一個int 值,等於-1 表示流沒有聯絡的緩衝或操作失敗。