views:

4266

answers:

6

I created a C++ console app and just want to capture the cout/cerr statements in the Output Window within the Visual Studio 2005 IDE. I'm sure this is just a setting that I'm missing. Can anyone point me in the right direction?

+7  A: 

You can't do this.

If you want to output to the debugger's output window, call OutputDebugString.

I found this implementation of a 'teestream' which allows one output to go to multiple streams. You could implement a stream that sends data to OutputDebugString.

Mike Dimmick
-1, wrong. See Ben's answer.
MSalters
A: 

Is this a case of the output screen just flashing and then dissapearing? if so you can keep it open by using cin as your last statement before return.

No you cannot because evaluating cin on its own has no effect.
ben
+3  A: 

You can capture the output of cout like this, for example:

std::streambuf* old_rdbuf = std::cout.rdbuf();
std::stringbuf new_rdbuf;
// replace default output buffer with string buffer
std::cout.rdbuf(&new_rdbuf);

// write to new buffer, make sure to flush at the end
std::cout << "hello, world" << std::endl;

std::string s(new_rdbuf.str());
// restore the default buffer before destroying the new one
std::cout.rdbuf(old_rdbuf);

// show that the data actually went somewhere
std::cout << s.size() << ": " << s;

Magicking it into the Visual Studio 2005 output window is left as an exercise to a Visual Studio 2005 plugin developer. But you could probably redirect it elsewhere, like a file or a custom window, perhaps by writing a custom streambuf class (see also boost.iostream).

ben
No plugin needed, just use OutputDebugString as mentioned by Mike Dimmick.
jwfearn
+1  A: 

A combination of ben's answer and Mike Dimmick's: you would be implementing a stream_buf_ that ends up calling OutputDebugString. Maybe someone has done this already? Take a look at the two proposed Boost logging libraries.

Adam Mitz
A: 

Also, depending on your intentions, and what libraries you are using, you may want to use the TRACE macro (MFC) or ATLTRACE (ATL).

ee
A: 

Displaying cin at the end does not allow destructors to be seen.

Any way out, to visually see the destructors code?

Display