对于C++的I/O流和各自的缓冲区的理解

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

int main()
{
        ostream hexout(cout.rdbuf());
        hexout.setf(ios::hex, ios::basefield);
        hexout.setf(ios::showbase);

        hexout << "hexout: " << 177 << " ";
        //cout << "cout: " <<177 << " ";
        //hexout << endl;
        cout << endl;

        istream hexin(cin.rdbuf());
        hexin.setf(ios::hex, ios::basefield);
        hexin.setf(ios::showbase);

        int x;
        hexin >>x;
        cout << x <<endl;
        return 0;
}
hexout: 0xb1 
F
15

当向一个输出流写入数据的时候,数据会先执行输出流所定义的格式化操作,之后才会写入其对应的缓冲区中.

当一个输入流读入数据的时候,数据会先写入缓冲区中,之后将缓冲区中的数据读出执行其输入流对应的格式化操作.

posted @ 2021-11-19 17:50  刘大侠GG_B  阅读(43)  评论(0编辑  收藏  举报