Connecting Tech Pros Worldwide Forums | Help | Site Map

Redirect cout

Hai Ly Hoang [MT00KSTN]
Guest
 
Posts: n/a
#1: Nov 16 '05
I have a C++ console program which has a lot of output (no input) like this:
cout << "Strings here".

Now i want to turn this application to windows application (with gui) and I
want to bring the output of
cout << "string" to a rich text box. Because of a great quantity of cout, i
don't and can't change each cout << code.
I need some mechansim (like pipe or hook into << command) to direct the
output of cout << into the rich text box.
Anyone know about it ?

Thanks a lot.



KC
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Redirect cout


Try this:

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

void redirect()
{
cout << "the first row" << endl;
streambuf* strm_buffer = strm.rdbuf();

//Manipulate your streambuf here
}

streambuf consists of stream data held entirely within an in-memory byte
array. You can manipulate it, for example, write to file.

Regards,
KC

" Hai Ly Hoang [MT00KSTN]" <ReplyToGroup@Mail.com> wrote in message
news:urFRsQ8uDHA.3496@TK2MSFTNGP11.phx.gbl...[color=blue]
> I have a C++ console program which has a lot of output (no input) like[/color]
this:[color=blue]
> cout << "Strings here".
>
> Now i want to turn this application to windows application (with gui) and[/color]
I[color=blue]
> want to bring the output of
> cout << "string" to a rich text box. Because of a great quantity of cout,[/color]
i[color=blue]
> don't and can't change each cout << code.
> I need some mechansim (like pipe or hook into << command) to direct the
> output of cout << into the rich text box.
> Anyone know about it ?
>
> Thanks a lot.
>
>[/color]


tom_usenet
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Redirect cout


On Sat, 6 Dec 2003 13:42:14 +0700, " Hai Ly Hoang [MT00KSTN]"
<ReplyToGroup@Mail.com> wrote:
[color=blue]
>I have a C++ console program which has a lot of output (no input) like this:
>cout << "Strings here".
>
>Now i want to turn this application to windows application (with gui) and I
>want to bring the output of
>cout << "string" to a rich text box. Because of a great quantity of cout, i
>don't and can't change each cout << code.
>I need some mechansim (like pipe or hook into << command) to direct the
>output of cout << into the rich text box.
>Anyone know about it ?[/color]

The easiest way would be to write a streambuf that outputs to a rich
text box. Here's a skeleton:

#include <streambuf>

class textbox_streambuf: public std::streambuf
{
public:
textbox_streambuf(TextBoxHandle handle)
:m_handle(handle)
{
}

protected:
virtual int overflow(int c = EOF)
{
if (c != EOF)
{
char cc = traits_type::to_char_type(c);
//here you should output cc to text box.
//return EOF if you have an error
if (!OutputToTextBox(m_handle, cc))
{
return EOF;
}
}

return traits_type::not_eof(c);
}

private:
TextBoxHandle m_handle;
};

//and then to use it:

int main()
{
textbox_streambuf textbox_buf(mywindowhandle);
std::streambuf* oldbuf = std::cout.rdbuf(&textbox_buf);

//important to do this before textbox_buf is destroyed:
std::cout.rdbuf(oldbuf);
}

You can add buffering if you have performance problems with writing a
single character at a time to the text box, but that complicates the
implementation of the streambuf a little.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Closed Thread


Similar .NET Framework bytes