C++ 如何从 MFC 程序写入标准输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5094502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:22:08  来源:igfitidea点击:

how do I write to stdout from an MFC program?

c++mfcstdout

提问by Stephen Kellett

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.

MFC 程序通常不能写入标准输出。MFC 在启动过程中对 stdout/stdin 管道做了一些奇怪的事情,你写的任何东西(例如执行 printf("hello");)都会转到 /dev/null。

Does anyone know how to successfully write to stdout from an MFC program?

有谁知道如何从 MFC 程序成功写入标准输出?

Thanks for reading.

谢谢阅读。

采纳答案by Nawaz

Use AllocConsolefunction to create a console for writing into. The following article explains how to use it to print to console.

使用AllocConsole函数创建一个用于写入的控制台。下面的文章解释了如何使用它打印到控制台。

Creating a console for your MFC app's debug output

为 MFC 应用程序的调试输出创建控制台

Dont forget to FreeConsoleonce you're done with it.

完成后不要忘记使用FreeConsole

回答by John K

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.

这是我不久前在网上找到的一个单行,它将标准输出附加到 MFC 中的控制台。这允许 printf 和 cout 写入当前进程的控制台窗口。我从来没有研究过它是如何工作的,所以如果你需要一个 cerr 或 cin 版本,你就靠自己了。

AllocConsole();
*stdout = *_tfdopen(_open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), _O_APPEND), _T("a"));

回答by noelicus

This will attach to the calling console window, if one is present. GotConsoleAttachwill be FALSEwhen the application wasn't called from a console.

这将附加到调用控制台窗口(如果存在)。GotConsoleAttach将是FALSE在未从控制台调用应用程序时。

GotConsoleAttach = FALSE;    
if (AttachConsole(ATTACH_PARENT_PROCESS))
{   
    int osfh = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), 8);
    if ((HANDLE)osfh != INVALID_HANDLE_VALUE)
    {
        *stdout = *_tfdopen(osfh, _T("a"));
        GotConsoleAttach = TRUE;
    }
}

回答by JasonH

If you are just looking for output to the debug window, you can use TRACE.

如果您只是在寻找调试窗口的输出,则可以使用 TRACE。

TRACE("This is a debug string of text in MFC");

I do this when I am quickly testing something and don't want to use dialog boxes, like MessageBox("text").

当我快速测试某些东西并且不想使用对话框时,我会这样做,例如 MessageBox("text")。

回答by Joao Louzada

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

在花了一整天试图让我的 MFC 程序使用 printf() 和 cout 进行打印后,我终于找到了一个解决方案并决定将其发布在这里以帮助想要在 MFC 打印的人...

void EnablePrintfAtMFC()
{
    if (AttachConsole(ATTACH_PARENT_PROCESS))
    {
        FILE* pCout;
        freopen_s(&pCout, "CONOUT$", "w", stdout);
        std::cout.clear();
        std::wcout.clear();
    }
}

Just call the above function in some place of your program, and after that you will be able to use printf() and cout...

只需在程序的某个地方调用上述函数,然后就可以使用 printf() 和 cout...

EnablePrintfAtMFC();
printf("Hello world!\n");
std::cout << "It works!" << endl;