zoukankan      html  css  js  c++  java
  • C++ Primer 读书笔记 第八章

    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 stream's 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;  //inserts a null, then flushes the buffer
    cout << "Hello" << endl;   //insert 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 from 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 we're 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

  • 相关阅读:
    bzoj3524: [Poi2014]Couriers(主席树)
    51nod 1275 连续子段的差异(twopointer+单调队列)
    51nod 1274 最长递增路径(DP)
    51nod 1273 旅行计划(思维题)
    51nod 1257 背包问题 V3(分数规划)
    CSS 几款比较常用的翻转特效
    css文字飞入效果
    jQuery使用方法
    数据库
    关系型数据库基本概念及MySQL简述
  • 原文地址:https://www.cnblogs.com/null00/p/3100897.html
Copyright © 2011-2022 走看看