不在浮沙筑高台-demons
C++/OS/disassembly/Anti-virus
posts - 5,  comments - 5,  trackbacks - 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<<dwin.rdbuf(); //这条语句可以直接输出"redirection.txt"的全部内容,这是rdbuf()的另一个强大功能。

下面是完整验证代码,第一次打开文件写 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 <iostream>
#include <fstream>
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.rdbuf();//Note: this sentence can output whole file
    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<<dwin.rdbuf();
    cout<<endl;
  //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”,

编译器是不会放行的,当然,首先能想到的原因就是ostream没有这样的无参构造函数,于是我打开 ostream 文档,里面有两个构造函数,如下:

 	
 explicit  basic_ostream(basic_streambuf<_Elem, _Traits> *_Strbuf,bool _Isstd = false)
	
 basic_ostream(_Uninitialized, bool _Addit = true)

可是当时的我还不能看懂这个声明的意思,于是我就想着去看看cout的定义,于是我就点击cout-转到定义,得到的是__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2 ostream cout;然后我把整个ostream文件搜了个遍,但还是没找到,说了是全局对象,但又怎么也找不到,真欲抱琵琶半遮面,千呼万唤不出现,当时我用了一种变通的方法创造出了我的myout, 怎么做的呢,你猜猜吧,呵呵,言归正传.其实我后来挖掘到这里的时候,产生了一个想法,仔细看我上面的代码,我是先创建一个文件,然后把cout的流重定向到文件,再来看看ostream的第一个构造函数,我笑了,你想到了吗...下面直接给出代码:

//my cout
ofstream console(stdout);
ostream mycout(console.rdbuf());
//my cin
ifstream  input(stdin);
ostream myin(input.rdbuf());
//my cerr
ofstream error(stderr);
ostream mycerr(error.rdbuf());
好了,本文到此也应该收笔了, 需要提醒读者的是,io类族中除过ios_base类外其他的都可以用rdbuf进行重定向,而不仅限于cout,本文只是以其示例.还有就是通过重定向可以方便实现其它很多功能,
比如用一个cin语句就可以把键盘输入写道文件等等,读者应该能举一反三.最后,若复制转载请注明原作者,请支持原作.
 
posted on 2012-05-11 12:20 demons 阅读(2938) 评论(0)  编辑 收藏 引用 所属分类: The standard C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜