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

后端 未结 8 2153
醉话见心
醉话见心 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:32

    I'd like to give my 2 cents.

    Given that the maybe is a VS issue about compliancy with the C++ standard or that we could use OutputDebugStringA, if you cannot modify your code base you may like the idea of simply redirect the std::cout to something else, like a file.

    So without changing your code base you can do something like suggested here:How to redirect cin and cout to files?

    Condensed:

    • add the include #include
    • at the beginning of your app, in some init, before logging you can use:
    std::ofstream out("out.txt");
    std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
    std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
    
    • the at the end of your app/logging:

    std::cout.rdbuf(coutbuf); //reset to standard output again

    Hope this may help someone, Kudos to Nawaz that provided the answer in the other thread.

提交回复
热议问题