The IO Library標(biāo)準(zhǔn) IO 庫
8.1 An Object-Oriented Library
標(biāo)準(zhǔn)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 條件狀態(tài)Condition States
條件狀態(tài)成員是做什么用的?
是用來詳細(xì)地描述當(dāng)前的流(stream)的狀態(tài)。例如:是否可用,或者遇到了某種特殊的錯誤。這些狀態(tài)信息即可以訪問,也可以設(shè)置。
每個流對象(stream object)都會包含一個成員(member),這個成員通過setstate和clear操作進行管理。它的類型是iostate。
每個 IO 類還定義了三個 iostate 類型的常量值,分別表示特定的位模式。
strm::badbit:badbit 標(biāo)志著系統(tǒng)級的故障,如無法恢復(fù)的讀寫錯誤。
strm::failbit:如果出現(xiàn)的是可恢復(fù)的錯誤,如在希望獲得數(shù)值型數(shù)據(jù)時輸入了字符,此時則設(shè)置 failbit 標(biāo)志。
strm::eofbit:是在遇到文件結(jié)束符時設(shè)置的,此時同時還設(shè)置了 failbit。
對于IO類的條件狀態(tài)的操作,不管是查詢的,eof(),fail(),bad(),還是賦值操作,clear(),setstate(),應(yīng)該都是位操作。
8.3 管理輸出緩沖 Managing the Output Buffer
哪些條件下buffer會被清空?
1. The program completes normally. All output buffers are emptied as part of the return from main.
程序正常結(jié)束。作為 main 返回工作的一部分,將清空所有輸出緩沖區(qū)。
2. At some indeterminate time, the buffer can become full, in which case it will be flushed before writing the next value.
在一些不確定的時候,緩沖區(qū)可能已經(jīng)滿了,在這種情況下,緩沖區(qū)將會在寫下一個值之前刷新。
3. We can flush the buffer explicitly using a manipulator such as endl, flush, ends.
用操縱符顯式地刷新緩沖區(qū),例如行結(jié)束符 endl。
4. We can use the unitbuf manipulator to set the stream's internal state to empty the buffer after each output operation.
在每次輸出操作執(zhí)行完后,用 unitbuf 操作符設(shè)置流的內(nèi)部狀態(tài),從而清空緩沖區(qū)。
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.
可將輸出流與輸入流關(guān)聯(lián)(tie)起來。在這種情況下,在讀輸入流時將刷新其關(guān)聯(lián)的輸出緩沖區(qū)。
程序崩潰時,緩沖區(qū)是不會被刷新的
前提:如果程序非正常結(jié)束,輸出緩沖(Output buffer)是不會刷新的。
建議:在每個輸出都用endl的結(jié)尾。
關(guān)于endl和"n的區(qū)別?
如果簡單的從輸出上看,二者肯定是沒有區(qū)別的。但是endl會刷新輸出cout的buffer。
輸入流和輸出流綁定
cin和cout綁定
結(jié)果是:任何讀輸入流的嘗試都將首先刷新與之綁定的輸出流的緩沖區(qū)(buffer)。:
cin.tie(&cout);
cin.tie(0); //break tie to cout, cout no longer flushed when cin is read
|
交互系統(tǒng)一定要保證輸出流和輸入流是綁定在一起的。
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風(fēng)格的字符串。
文件流可以和文件重新綁定
l 文件流可以和文件重新綁定。但是在重新綁定前要先關(guān)閉流,并清空文件流的狀態(tài),否則上一次的文件流的狀態(tài)就會是重新綁定后的初始狀態(tài)。
l 關(guān)閉流對象,都不可能改變流對象內(nèi)部的狀態(tài)。Closing a stream does not change the internal state of the stream object.
文件模式
ios_base::openmode
truncate
當(dāng)文件打開時清空文件的所有內(nèi)容,如果使用這個屬性對文件至少要有寫入的權(quán)限
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
}
|
這是一個標(biāo)準(zhǔn)的文件重新綁定的代碼,包括了以上的注意事項。
8.5 字符串流String Streams
字符串流和文件流其實很類似。獨有的就是利用字符串流來實現(xiàn)格式化輸入/輸出。
輸出
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
|