在Visual Studio 2005输出窗口中捕获cout? [英] Capturing cout in Visual Studio 2005 output window?

查看:415
本文介绍了在Visual Studio 2005输出窗口中捕获cout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个C ++控制台应用程序,只想捕获Visual Studio 2005 IDE中输出窗口中的cout / cerr语句。我相信这只是一个设置,我错过了。任何人都可以指向正确的方向?

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?

推荐答案

我终于实现了这个,所以我想和大家分享:

I've finally implemented this, so I want to share it with you:

#include <vector>
#include <iostream>
#include <windows.h>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/tee.hpp>

using namespace std;
namespace io = boost::iostreams;

struct DebugSink
{
    typedef char char_type;
    typedef io::sink_tag category;

    std::vector<char> _vec;

    std::streamsize write(const char *s, std::streamsize n)
    {
        _vec.assign(s, s+n);
        _vec.push_back(0); // we must null-terminate for WINAPI
        OutputDebugStringA(&_vec[0]);
        return n;
    }
};

int main()
{
    typedef io::tee_device<DebugSink, std::streambuf> TeeDevice;
    TeeDevice device(DebugSink(), *cout.rdbuf());
    io::stream_buffer<TeeDevice> buf(device);
    cout.rdbuf(&buf);

    cout << "hello world!\n";
    cout.flush(); // you may need to flush in some circumstances
}

如果您写:

X:\full\file\name.txt(10) : message

到输出窗口,然后双击它,Visual Studio将跳转到给定的文件,第10行,并在状态栏中显示消息。 非常有用

to the output window and then double-click on it, then Visual Studio will jump to the given file, line 10, and display the 'message' in status bar. It's very useful.

这篇关于在Visual Studio 2005输出窗口中捕获cout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆