<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    PS,1880后程序員

    看不完的牙,寫不完的程序,跑不完的步。
    隨筆 - 97, 文章 - 34, 評論 - 10, 引用 - 0
    數據加載中……

    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),這個成員通過setstateclear操作進行管理。它的類型是iostate

    每個 IO 類還定義了三個 iostate 類型的常量值,分別表示特定的位模式。

    strm::badbitbadbit 標志著系統級的故障,如無法恢復的讀寫錯誤。

    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會刷新輸出coutbuffer

    輸入流和輸出流綁定

    cincout綁定

    結果是:任何讀輸入流的嘗試都將首先刷新與之綁定的輸出流的緩沖區(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 閱讀(482) 評論(2)  編輯  收藏 所屬分類: C++ Primer 之 讀書筆記

    評論

    # re: C++ Primer 之 讀書筆記 第八章  回復  更多評論   

    8.5 字符串流String Streams
    這個實驗你做出來了嗎?我把輸入那一行用val3,val4代替,實驗結果就不對了。
    int val3 =0, val4 = 0;
    input_istring >> dump >> val3 >> dump >> val4;
    cout << val1 << " " << val2 << endl; // prints 0 0
    可見那個輸入流的語句根本就沒起作用。
    2010-08-15 14:58 | qq359115139

    # re: C++ Primer 之 讀書筆記 第八章  回復  更多評論   

    這行也要改
    cout << val3 << " " << val4 << endl; // prints 0 0
    2010-08-15 14:59 | qq359115139
    主站蜘蛛池模板: 亚洲免费视频在线观看| 亚洲av无码国产精品色午夜字幕 | 日韩激情淫片免费看| 亚洲av无码一区二区三区在线播放| 免费不卡中文字幕在线| 好男人资源在线WWW免费| 777亚洲精品乱码久久久久久 | 毛片免费观看的视频| 免费一区二区三区在线视频| 久久精品国产精品亚洲艾| 成人毛片免费观看视频| 99精品视频在线视频免费观看| 国产高清视频免费在线观看| 一级免费黄色大片| 两个人看的www免费视频| 成年大片免费视频播放一级| 特级毛片aaaa级毛片免费| 人碰人碰人成人免费视频| 黄页视频在线观看免费| 一级黄色毛片免费看| 在线观看免费无码视频| 99久久免费国产精精品| 午夜理伦剧场免费| 色妞WWW精品免费视频| 免费一级毛片正在播放| 亚洲AV日韩AV天堂久久| 精品视频免费在线| 亚洲午夜国产精品| 亚洲综合网站色欲色欲| 最近中文字幕无免费视频| 午夜无码A级毛片免费视频| 免费精品国产自产拍在线观看 | 亚洲a一级免费视频| 免费99热在线观看| 国产99视频精品免费观看7| 久久免费国产精品一区二区| 色婷婷综合缴情综免费观看| 亚洲色中文字幕在线播放| 亚洲精品国产福利在线观看| 国产亚洲精品无码成人| 亚洲国产成人久久综合碰|