Capturing cout in Visual Studio 2005 output window?

后端 未结 7 1554
北海茫月
北海茫月 2020-12-05 15:15

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

相关标签:
7条回答
  • 2020-12-05 15:36

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

    0 讨论(0)
提交回复
热议问题