C++ Primer 之 讀書筆記 第八章
The IO Library標準 IO 庫
8.1 An Object-Oriented Library
標準IO庫實際上是針對三類流stream的操作
stream |
Header |
Type |
|
console |
iostream |
istream ostream iostream |
提供 char wchar_t cin wcin … |
string stream |
sstream |
istringstream ostringstream stringstream |
|
file |
fstream |
ifstream ofstream fstream |
對于IO對象來說是不能拷貝或賦值的,因此這些寫法都是會死人的:
ofstream out1, out2; out1 = out2; // error: cannot assign stream objects ofstream print(ofstream); out2 = print(out2); // error: cannot copy stream objects,形參不能是IO類型 |
l 形參不能是IO類型。返回值也不能是IO類型。
l IO對象也不能保存在vector這樣的集合容器里。
8.2 條件狀態Condition States
條件狀態成員是做什么用的?
是用來詳細地描述當前的流(stream)的狀態。例如:是否可用,或者遇到了某種特殊的錯誤。這些狀態信息即可以訪問,也可以設置。
每個流對象(stream object)都會包含一個成員(member),這個成員通過setstate和clear操作進行管理。它的類型是iostate。
每個 IO 類還定義了三個 iostate 類型的常量值,分別表示特定的位模式。
strm::badbit:badbit 標志著系統級的故障,如無法恢復的讀寫錯誤。
strm::failbit:如果出現的是可恢復的錯誤,如在希望獲得數值型數據時輸入了字符,此時則設置 failbit 標志。
strm::eofbit:是在遇到文件結束符時設置的,此時同時還設置了 failbit。
對于IO類的條件狀態的操作,不管是查詢的,eof(),fail(),bad(),還是賦值操作,clear(),setstate(),應該都是位操作。
8.3 管理輸出緩沖 Managing the Output Buffer
哪些條件下buffer會被清空?
1. The program completes normally. All output buffers are emptied as part of the return from main.
程序正常結束。作為 main 返回工作的一部分,將清空所有輸出緩沖區。
2. At some indeterminate time, the buffer can become full, in which case it will be flushed before writing the next value.
在一些不確定的時候,緩沖區可能已經滿了,在這種情況下,緩沖區將會在寫下一個值之前刷新。
3. We can flush the buffer explicitly using a manipulator such as endl, flush, ends.
用操縱符顯式地刷新緩沖區,例如行結束符 endl。
4. We can use the unitbuf manipulator to set the stream's internal state to empty the buffer after each output operation.
在每次輸出操作執行完后,用 unitbuf 操作符設置流的內部狀態,從而清空緩沖區。
5. We can tie the output stream to an input stream, in which case the output buffer is flushed whenever the associated input stream is read.
可將輸出流與輸入流關聯(tie)起來。在這種情況下,在讀輸入流時將刷新其關聯的輸出緩沖區。
程序崩潰時,緩沖區是不會被刷新的
前提:如果程序非正常結束,輸出緩沖(Output buffer)是不會刷新的。
建議:在每個輸出都用endl的結尾。
關于endl和"n的區別?
如果簡單的從輸出上看,二者肯定是沒有區別的。但是endl會刷新輸出cout的buffer。
輸入流和輸出流綁定
cin和cout綁定
結果是:任何讀輸入流的嘗試都將首先刷新與之綁定的輸出流的緩沖區(buffer)。:
cin.tie(&cout); cin.tie(0); //break tie to cout, cout no longer flushed when cin is read |
交互系統一定要保證輸出流和輸入流是綁定在一起的。
8.4 文件輸入和輸出 File Input and Output
fstream header
l ifstream
l ofstream
l fstream
基本語法格式:
// construct an ifstream and bind it to the file named ifile ifstream infile(ifile.c_str()); // ofstream output file object to write file named ofile ofstream outfile(ofile.c_str()); |
注意:文件名要使用C風格的字符串。
文件流可以和文件重新綁定
l 文件流可以和文件重新綁定。但是在重新綁定前要先關閉流,并清空文件流的狀態,否則上一次的文件流的狀態就會是重新綁定后的初始狀態。
l 關閉流對象,都不可能改變流對象內部的狀態。Closing a stream does not change the internal state of the stream object.
文件模式
ios_base::openmode
truncate
當文件打開時清空文件的所有內容,如果使用這個屬性對文件至少要有寫入的權限
Sample:
// opens in binding it to the given file ifstream& open_file(ifstream &in, const string &file) { in.close(); // close in case it was already open in.clear(); // clear any existing errors // if the open fails, the stream will be in an invalid state in.open(file.c_str()); // open the file we were given return in; // condition state is good if open succeeded } |
這是一個標準的文件重新綁定的代碼,包括了以上的注意事項。
8.5 字符串流String Streams
字符串流和文件流其實很類似。獨有的就是利用字符串流來實現格式化輸入/輸出。
輸出
int val1 = 512, val2 = 1024; ostringstream format_message; // ok: converts values to a string representation format_message << "val1: " << val1 << ""n" << "val2: " << val2 << ""n"; |
輸入:
// str member obtains the string associated with a stringstream istringstream input_istring(format_message.str()); string dump; // place to dump the labels from the formatted message // extracts the stored ascii values, converting back to arithmetic types input_istring >> dump >> val1 >> dump >> val2; cout << val1 << " " << val2 << endl; // prints 512 1024 |
posted on 2009-05-25 17:29 amenglai 閱讀(479) 評論(2) 編輯 收藏 所屬分類: C++ Primer 之 讀書筆記