• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            不在浮沙筑高臺-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 閱讀(2979) 評論(0)  編輯 收藏 引用 所屬分類: The standard C++

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            思思久久好好热精品国产| 国产亚洲综合久久系列| 久久露脸国产精品| 一级做a爰片久久毛片毛片| 久久婷婷五月综合色奶水99啪| 久久精品国产亚洲AV香蕉| 亚洲AV无码久久| 中文字幕成人精品久久不卡 | 国产精品免费久久久久影院| 久久久噜噜噜久久中文字幕色伊伊| 亚洲精品乱码久久久久久蜜桃| 国产三级久久久精品麻豆三级| 国产精品伊人久久伊人电影| 久久男人Av资源网站无码软件| 久久久久国产亚洲AV麻豆| 久久久久久久久无码精品亚洲日韩 | 亚洲精品无码久久久久sm| 久久亚洲欧美日本精品| 区久久AAA片69亚洲| 国产精品美女久久久免费| 欧洲精品久久久av无码电影| 国产精品99久久久久久人| 久久久久国产亚洲AV麻豆| 久久国产高清字幕中文| 国产亚洲精久久久久久无码77777| 精品久久香蕉国产线看观看亚洲 | 久久99热狠狠色精品一区| 久久综合偷偷噜噜噜色| 精品无码人妻久久久久久| 久久不射电影网| 性欧美大战久久久久久久久| 蜜桃麻豆WWW久久囤产精品| 欧洲国产伦久久久久久久| 久久精品无码av| 成人精品一区二区久久| 国产一级做a爰片久久毛片| 色综合久久无码中文字幕| 天天爽天天狠久久久综合麻豆| 久久精品国产亚洲AV香蕉| 蜜臀久久99精品久久久久久小说| 久久国产免费直播|