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

  • 相关阅读:
    nginx配置
    day5 业务开发中较有用代码
    day4 Vue基础
    npm vue的一些命令
    day3 ES6基础
    python_矩阵的加法和乘法计算(包括矩阵的动态输入,纯列表实现不引入其他模块)
    python_利用元组实现剪刀石头布
    python_整型与IP地址的转换
    python_判断标识符的合法性
    python_生成随机数与列表排序
  • 原文地址:https://www.cnblogs.com/null00/p/3100897.html
Copyright © 2011-2022 走看看