How can I see cout output in a non-console application?

后端 未结 8 2177
醉话见心
醉话见心 2021-02-06 22:10

It seems rather tedious to output to debug window. Where can I find cout output if I am writing a non-console information ?

Like:

double          


        
8条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-06 22:39

    SOLUTION: This answer solves the question and allows you to redirect console output to the Visual Studio Output window. First we need a class that overrides the default cout string stream:

    class dbg_stream_for_cout
        : public std::stringbuf
    {
    public:
        ~dbg_stream_for_cout() { sync(); }
        int sync()
        {
            ::OutputDebugStringA(str().c_str());
            str(std::string()); // Clear the string buffer
            return 0;
        }
    };
    dbg_stream_for_cout g_DebugStreamFor_cout;
    

    Then, somewhere you want to "activate" writing to the VS output window:

    std::cout.rdbuf(&g_DebugStreamFor_cout); // Redirect std::cout to OutputDebugString!
    

提交回复
热议问题