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

  • 相关阅读:
    scrollLeft、offsetLeft、clientLeft、clientHeight区分
    表格斜线
    td里的所有STYLE
    对输入框输入的数字进行格式控制,是很实用的效果
    MSComm控件使用详解
    如何用javascripr动态添加table?
    TeeChart的安装 delphi7
    DELPHI 字符串分割处理
    转正则表达式 简单
    求解:js函数在预编译期间的谜 简单
  • 原文地址:https://www.cnblogs.com/null00/p/3100897.html
Copyright © 2011-2022 走看看