• <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 閱讀(2980) 評論(0)  編輯 收藏 引用 所屬分類: The standard C++

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

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲乱码日产精品a级毛片久久| 色综合久久综合中文综合网| 国产三级精品久久| 久久天天躁狠狠躁夜夜2020老熟妇| 日韩欧美亚洲综合久久影院Ds | 亚洲va国产va天堂va久久| 精品久久久久久成人AV| 久久精品国产精品亚洲人人 | 麻豆久久久9性大片| 漂亮人妻被黑人久久精品| 成人亚洲欧美久久久久| 少妇久久久久久久久久| 18岁日韩内射颜射午夜久久成人| 亚洲欧洲精品成人久久奇米网| 久久综合给久久狠狠97色 | 久久人人爽人人爽人人片AV麻烦 | 99久久久精品免费观看国产| 国产精品gz久久久| 久久久久久亚洲精品成人| 久久婷婷人人澡人人| 久久99精品国产99久久| 久久无码AV中文出轨人妻| 精品久久久久久国产免费了| 高清免费久久午夜精品| 一本色道久久HEZYO无码| 久久精品亚洲欧美日韩久久| 2021少妇久久久久久久久久| 色综合久久无码五十路人妻| 久久人人爽人人人人爽AV| 国产日韩久久免费影院| 亚洲成人精品久久| 久久这里只精品国产99热| 久久A级毛片免费观看| 久久久亚洲欧洲日产国码二区| 亚洲中文字幕无码久久2017 | 热99RE久久精品这里都是精品免费| 久久成人精品视频| 99久久超碰中文字幕伊人| 国产精品国色综合久久| 99精品国产在热久久无毒不卡| 日韩人妻无码精品久久久不卡|