Alex Rivera | Logout

How do I get console output in C++ with a Windows program?

Asked 2008-10-10T15:22:14.473
50

If I have a native C++ windows program (i.e. the entry point is WinMain) how do I view output from console functions like std::cout?

Edit
Report

2 Answers

18

You can also reopen the cout and cerr streams to output to a file as well. The following should work for this:

#include <iostream>
#include <fstream>

int main ()
{
    std::ofstream file;
    file.open ("cout.txt");
    std::streambuf* sbuf = std::cout.rdbuf();
    std::cout.rdbuf(file.rdbuf());
    //cout is now pointing to a file
    return 0;
}
answered 2008-10-10T15:35:59.983
2

Don't quote me on this, but the Win32 console API might be what you're looking for. If you're just doing this for debugging purposes, however, you might be more interested in running DebugView and calling the DbgPrint function.

This of course assumes its your application you want sending console output, not reading it from another application. In that case, pipes might be your friend.

answered 2008-10-10T15:28:05.833

Your Answer