Redirect the stream buffer of cout to a file with ios::rdbuf() (一)

2014-11-24 12:06:10 · 作者: · 浏览: 0

Introduction:
This article mainly about how to use ios::rdbuf() function redirect the standard output stream to a file ,that is,redirect the cout output stream.
Two questions will be answered in this article: 1.how to redirect a stream. 2. what is the real face of cout,cin and cerr.
很多时候,我们为了实现一些特殊的目的,都会涉及到给控制台输出重定向的问题,比如在编写GUI程序时,为了调试方便,我们需要把一些中间结果输出,但如果用MessageBox,
真是差强人意,输个字符串还凑合,但如果要输出个数值或者地址,我们就不得不创建一个CString对象将它们格式化成一个字符串再用MessageBox输出,这可真辛苦!当然每个人的
调试方法都不一样,各有绝招。本文主要讲述如何将 cout输出流重定向到其他地方,比如一个文件,当然如果你想在自己的GUI工程中把一些调试数据通过cout输出到一个文件,或者
直接用cout写日志,那么本文将会对你有帮助,其次如果当你了解了重定向就、技术后,你将会重新认识一些老朋友比如:cout, cin, cerr的真正身份。
首先我们介绍一位 basic_ios 类中非常重要的一位成员,他就是ios::rdbuf().我们先来看看这个函数的定义(vs2008):
typedef basic_streambuf<_Elem, _Traits> _Mysb;
_Mysb *rdbuf() const
{ // return stream buffer pointer
return (_Mystrbuf);
}
_Mysb * rdbuf(_Mysb *_Strbuf)
{ // set stream buffer pointer
_Mysb *_Oldstrbuf = _Mystrbuf;
_Mystrbuf = _Strbuf;
clear();
return (_Oldstrbuf);
}
不难看出rdbuf()函数正是设置和修改io流指针的,正因为这个原因,我想到是否能通过修改io类关联的缓冲区指针对其输入输出进行重定向,当然事实是乐观的。作为示例,我对cout动
手术。首先,把cout的输出重定向到一个文件中,通过以下代码实现:
ofstream dwout( "redirection.txt" );
ofstream::_Mysb * org_cout= cout.rdbuf(dwout.rdbuf());
这样一来,就成功把cout的输出缓冲区流重定向到了"redirection.txt"文件,而不会输出到控制台,当然我们应该把原来流指针保存到 org_cout 里,用于恢复cout的流缓冲区。接下来
我们可以这样:
for (int i = 0; i < 5 ; i++)
{
cout<<"redirection ";
}
向"redirection.txt"中输入5个"redirection "成功后会输出到文件而不是控制台。接下来就是打开"redirection.txt"文件,把文件内容输出到屏幕上,所以我们必须先恢复cout流,使它
输出恢复到控制台,然后来验证我们的重定向是否成功,代码如下:
dwin.open( "redirection.txt");
cout< 下面是完整验证代码,第一次打开文件写 5个"duwen",然后输出是为了验证cout<< file.rdbuf()可以将文件所有内容一次性全部输出,紧接着是重定向cout,然后写cout 5个
"redirection",紧接着恢复cout流,将文件内容输出到控制台,由于默认打开方式是默认out,所以第二次打开文件写时(cout)会把文件内容先清空,也就是把5 个"duwen”清空,所以
第二次显示后,只显示5个"redirection" :

/************************************************************************
Description : Redirect the cout stream.
Notices : Copyright (c) Duwen
TIME : Post time: 5/11/2012, write time:6/3/2011
************************************************************************/

#include "stdafx.h"
#include
#include
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
//create a redirection file, and write 10 "duwen" in.

ofstream dwout("redirection.txt");
for (int i=0 ; i< 5 ; i++)
{
dwout<<"duwen ";
}
dwout<<'\n';
dwout.close();

//Output the file contents
ifstream dwin("redirection.txt");
cout< dwin.close();

//Open "redirection.txt", we'll redirect the stream buffer of "cout" latter.
dwout.open("redirection.txt");
//Redirect
ofstream::_Mysb * org_cout=cout.rdbuf(dwout.rdbuf());
for (int i = 0; i < 5 ; i++)
{
//We output 5 "redirection" which cannot be shown on console, instead, it will be redirected to "redirection.txt".
cout<<"redirection ";
}
dwout.close();
//open redirection.txt.
dwin.open("redirection.txt");
//recover the stream buffer of cout ,that is, console
cout.rdbuf(org_cout);

//verify if our redirection succeeded.
cout< cout< //Note: we mustn't close all the files we have opened, because the destructor of each file class did it automatically.
return 0;
}
好了,有了上面的了解,你是否还能联想到什么呢,探索永远都不会停止.
在我刚开始学习C++时,总是对cout,cin,cerr充满着好奇心,书上说它们都是一些全局流对象,就拿cout来说,书上说它是ostream的对象,但我试图这样 ostream myout 创造出我的 "cout”,时
编译器是不会放行的,当然,