MFC programs can't normally write to stdout. MFC does something weird with the stdout/stdin pipes during startup and anything you write (for example doing a printf("hello");) just goes to /dev/null.
Does anyone know how to successfully write to stdout from an MFC program?
Thanks for reading.
This will attach to the calling console window, if one is present.
GotConsoleAttach
will beFALSE
when the application wasn't called from a console.If you are just looking for output to the debug window, you can use TRACE.
I do this when I am quickly testing something and don't want to use dialog boxes, like MessageBox("text").
Here's a one-liner that I found online a while back that attaches stdout to a console in MFC. This allows printf and cout to write to the console window of the current process. I never looked into how it works, so if you need a cerr or cin version you're on your own.
Use AllocConsole function to create a console for writing into. The following article explains how to use it to print to console.
Creating a console for your MFC app's debug output
Dont forget to FreeConsole once you're done with it.
After spending an entire day trying to make my MFC program to print using printf() and cout I finally found a solution and decide to post it here to help who wants to print at MFC...
Just call the above function in some place of your program, and after that you will be able to use printf() and cout...