} } }

    C++ Primer 读书笔记 - 第八章

    添加时间:2013-5-28 点击量:

    1. IO library types do not allow copy or assignment.


        Only element types that support copy can be stored in vectors or other container types.


        We cannot have a parameter or return type that is one of the stream types. If we need to pass or return an IO object, it must be passed or returned as a pointer or reference.


    2. Condition States



    #include <iostream>
    
    #include
    <fstream>
    #include
    <sstream>
    #include
    <stdexcept>
    using namespace std;

    int main()
    {
    int val1 = 512, val2 = 1024;
    ostringstream format_message;
    format_message
    << val1: << val1 << \n
    << val2: << val2 << \n;

    istringstream input_string(format_message.str());
    string dump;
    input_string
    >> dump >> val1 >> dump >> val2;
    cout
    << val1 << << val2 << endl;


    int ival;
    while (cin >> ival, !cin.eof()) {
    if (cin.bad()) {
    throw runtime_error(IO stream corrupted);
    }
    else if (cin.fail()) {
    cerr
    << bad data, try again << endl;
    cin.clear(istream::goodbit);
    cin.ignore();
    continue;
    }
    else {
    cout
    << Input is << ival << endl;
    }
    }
    return 0;
    }


    3. Five ways to cause the buffer to be flushed.


        - The program completes normally.


        - The buffer becomes full.


        - Using a maipulator, such as endl, flush...


        - Use the unitbuf manipulator to set the streams internal state to empty the buffer after each output operation.


        - Tie the output stream to an input stream, in which case the output buffer is flushed whenever the associated input stream is read.



    cout << Hello << flush;  //flushes the buffer, adds no data
    
    cout << Hello << ends; //s a null, then flushes the buffer
    cout << Hello << endl; // a newline, then flushes the buffer

    cout
    << unitbuf << first << second << nounitbuf;
    same
    as
    cout
    << fisrt << flush << second << flush;


    4. Supplying a file name as an initializer to an ifstream or ofstream object has the effect of opening the specified file.


        To associate the fstream with a different file, we must first close the existing file and then open a different file.


        Closing a stream does not change the internal state of the stream object.


        If we reuse a file stream to read or write more thant one file, we must clear the stream before using it to read another file.



    ifstream input;
    
    vector
    <string>::const_iterator it = files.begin();

    while (it != files.end()) {
    input.open(it
    ->c_str()); //open the file
    if (!input)
    break;
    while (input >> s)
    process(s);
    input.close();
    //close file when were done with it
    input.clear(); //reset state to OK
    ++it;
    }


    5. The only way to preserve the existing data in a file opened by an ofstream by an ofstream is to specify app mode explicitly.


        in


        out


        app


        ate


        trunc


        binary

    彼此相爱,却不要让爱成了束缚:不如让它成为涌动的大海,两岸乃是你们的灵魂。互斟满杯,却不要同饮一杯。相赠面包,却不要共食一个。一起歌舞欢喜,却依然各自独立,相互交心,却不是让对方收藏。因为唯有生命之手,方能收容你们的心。站在一起却不要过于靠近。—— 纪伯伦《先知》
    分享到: