I am using microsoft visual studio to do C++. I don't see std::err and std::out in the output console of the IDE. Is there a way to redirect them ?
3 Answers
You can indeed redirect std::out and std::err. Simply right click on your project in the solution explorer and select Properties
. Then select Configuration Properties -> Debugging
and put the appropriate arguments into the Command Arguments
field. For example, to redirect std::err to a file, I would type in 2> ErrorLog.txt
.
The things you type in Command Arguments
simply get appended as command line arguments when Visual Studio runs your program, just like you had manually typed them in to the console. So, the above example simply tells VisualStudio to run your program with the command <programName>.exe 2> ErrorLog.txt
instead of just <programName>.exe
.

- 39,737
- 6
- 87
- 123

- 1,747
- 17
- 15
-
5Sorry my aim was to redirect the std::out and std::err to the output window of visual studio, not to a file. Thanks anyway – 0x26res Aug 07 '11 at 08:18
-
2@jules No problem. But, the title of the question is such that people just looking to redirect output might end up here as well, so I figured it would be worth posting this answer as well. – Brandon Aug 07 '11 at 22:18
-
@Brandon Is it `>2` or `2>`? – allonhadaya Jul 22 '13 at 20:14
-
It should be 2>filename.txt. If I understand things correctly, >2 would redirect stdout to a file named '2' because it defaults to redirecting stdout if you don't specify a file descriptor before the >. See [here](http://tldp.org/LDP/abs/html/io-redirection.html) for more info on IO redirection. – Brandon Jul 31 '13 at 07:06
I know this is an old thread but I can't help but giving the answer since I can't believe there still is no real answer. What you can do is redirect the cout to a ostringstream of your choice. To do this, derive a new class from streambuf that will send the stream to OutputDebugString (Lets call this class OutputDebugStream) and create an instance of the class, myStream. Now call:
cout.rdbuf(&myStream)
I used cout for an example. This same technique can be used with cerr, just call
cerr.rdbuf(&myStream).
Stdout is a little more difficult if you are not using cout. You can redirect stdout during runtime by using freopen()
, but it must be to a file. To get this to redirect to the Debug screen is a little more difficult. One way is to use fmemopen()
if it is available (it is not standard) and write a streambuf to output this data to the Debug screen. Alternatively, you can redirect to a file and write a stream to open as input and redirect to the debug stream. A bit more work, but I think it is possible.
I use the following macro for output to the visual studio console
#ifdef _MSC_VER
#include <Windows.h>
#include <iostream>
#include <sstream>
#include <opencv/cxcore.h>
#define DBOUT( s ) \
{ \
std::wostringstream os_; \
os_ << s; \
OutputDebugStringW( os_.str().c_str() ); \
}
#else
#define DBOUT( s ) \
{ \
std::cout << s; \
}
#endif
Now if I could only get it to work within a cuda kernel?!

- 1,047
- 17
- 20