<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
    數(shù)據(jù)加載中……

    C++ Primer 之 讀書筆記 第八章

     

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

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

    strm::badbitbadbit 標(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會刷新輸出coutbuffer

    輸入流和輸出流綁定

    cincout綁定

    結(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               

    posted on 2009-05-25 17:29 amenglai 閱讀(476) 評論(2)  編輯  收藏 所屬分類: C++ Primer 之 讀書筆記

    評論

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

    8.5 字符串流String Streams
    這個實驗?zāi)阕龀鰜砹藛幔课野演斎肽且恍杏胿al3,val4代替,實驗結(jié)果就不對了。
    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 之 讀書筆記 第八章  回復(fù)  更多評論   

    這行也要改
    cout << val3 << " " << val4 << endl; // prints 0 0
    2010-08-15 14:59 | qq359115139
    主站蜘蛛池模板: 日韩激情无码免费毛片| 日韩精品极品视频在线观看免费| 亚洲国产精品嫩草影院| 无码AV动漫精品一区二区免费 | 影音先锋在线免费观看| www.亚洲精品.com| 亚洲视频在线免费播放| 美女免费视频一区二区三区| 59pao成国产成视频永久免费| 亚洲成色在线影院| 1000部啪啪未满十八勿入免费| 亚洲欧洲国产日韩精品| 老司机午夜在线视频免费观| 国产午夜免费福利红片| 香蕉视频免费在线| 中文字幕亚洲不卡在线亚瑟| 一区免费在线观看| 亚洲成AV人片在线观看WWW| 99爱免费观看视频在线| 中文字幕亚洲综合小综合在线| 成熟女人特级毛片www免费| 亚洲视频在线观看网站| 美女网站免费福利视频| 亚洲国产欧洲综合997久久| 亚洲精品专区在线观看| 免费h视频在线观看| 国产成人麻豆亚洲综合无码精品| 三级网站在线免费观看| 亚洲高清在线mv| 四虎成人免费网址在线| 国产精品成人69XXX免费视频| 亚洲AV日韩精品一区二区三区 | 免费一级特黄特色大片在线| 一边摸一边桶一边脱免费视频| 日韩免费无码一区二区视频| 日本激情猛烈在线看免费观看| 国产18禁黄网站免费观看| 丝瓜app免费下载网址进入ios| 亚洲色无码一区二区三区| 1000部免费啪啪十八未年禁止观看| 亚洲国产欧美国产综合一区|