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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值