Redirect cout

  • Thread starter Hai Ly Hoang [MT00KSTN]
  • Start date
H

Hai Ly Hoang [MT00KSTN]

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.
 
K

KC

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
 
T

tom_usenet

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 ?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top