Redirect stdout and stderr to the output debug console of microsoft visual studio

后端 未结 3 460
鱼传尺愫
鱼传尺愫 2021-01-07 20:41

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条回答
  •  自闭症患者
    2021-01-07 21:25

    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.

提交回复
热议问题