简体   繁体   English

如何在 C++/Windows 中将 output 发送到控制台

[英]How to output to the console in C++/Windows

When using iostream in C++ on Linux, it displays the program output in the terminal, but in Windows, it just saves the output to a stdout.txt file. When using iostream in C++ on Linux, it displays the program output in the terminal, but in Windows, it just saves the output to a stdout.txt file. How can I, in Windows, make the output appear in the console?如何在 Windows 中使 output 出现在控制台中?

Since you mentioned stdout.txt I google'd it to see what exactly would create a stdout.txt;既然你提到了stdout.txt,我用谷歌搜索看看究竟是什么会创建一个stdout.txt; normally, even with a Windows app, console output goes to the allocated console, or nowhere if one is not allocated.通常,即使使用 Windows 应用程序,控制台 output 也会进入分配的控制台,如果没有分配控制台,则无处可去。

So, assuming you are using SDL (which is the only thing that brought up stdout.txt), you should follow the advice here .因此,假设您使用的是 SDL(这是唯一出现 stdout.txt 的内容),您应该遵循此处的建议。 Either freopen stdout and stderr with "CON", or do the other linker/compile workarounds there.使用“CON”打开标准输出和标准错误,或者在那里执行其他链接器/编译解决方法。

In case the link gets broken again, here is exactly what was referenced from libSDL:如果链接再次断开,这正是从 libSDL 中引用的内容:

How do I avoid creating stdout.txt and stderr.txt?如何避免创建 stdout.txt 和 stderr.txt?

"I believe inside the Visual C++ project that comes with SDL there is a SDL_nostdio target > you can build which does what you want(TM)." “我相信在 SDL 附带的 Visual C++ 项目中,有一个 SDL_nostdio 目标 > 您可以构建您想要的目标 (TM)。”

"If you define "NO_STDIO_REDIRECT" and recompile SDL, I think it will fix the problem." “如果你定义“NO_STDIO_REDIRECT”并重新编译SDL,我认为它会解决问题。” > > (Answer courtesy of Bill Kendrick) > > (答案由比尔·肯德里克提供)

You can add a console to a Windows non-console application using the process described in Adding Console I/O to a Win32 GUI App .您可以使用将控制台 I/O 添加到 Win32 GUI 应用程序中描述的过程将控制台添加到 Windows 非控制台应用程序。

There is a whole thread on gamedev.net on the topic. gamedev.net 上有一个关于该主题的完整主题。

For debugging in Visual Studio you can print to the debug console:对于 Visual Studio 中的调试,您可以打印到调试控制台:

OutputDebugStringW(L"My output string.");

If you have a none-console Windows application, you can create a console with the AllocConsole function.如果您有非控制台 Windows 应用程序,则可以使用AllocConsole function 创建控制台。 Once created, you can write to it using the normal std::cout methods.创建后,您可以使用普通的 std::cout 方法对其进行写入。

First off, what compiler or dev environment are you using?首先,您使用的是什么编译器或开发环境? If Visual Studio, you need to make a console application project to get console output.如果是Visual Studio,则需要制作一个console application project来获取console output。

Second,第二,

std::cout << "Hello World" << std::endl;

should work in any C++ console application.应该在任何 C++ 控制台应用程序中工作。

If you're using Visual Studio you need to modify the project property : Configuration Properties -> Linker -> System -> SubSystem .如果您使用的是 Visual Studio,则需要修改项目属性Configuration Properties -> Linker -> System -> SubSystem

This should be set to: Console (/SUBSYSTEM:CONSOLE)这应该设置为:控制台 (/SUBSYSTEM:CONSOLE)

Also you should change your WinMain to be this signature:此外,您应该将 WinMain 更改为此签名:

int main(int argc, char **argv)
{
    //...
    return 0;
}

The AllocConsole Windows API function will create a console window for your application. AllocConsole Windows API function 将为您的应用程序创建一个控制台 Z05B8C74CBD96FBF2DE4C1A352702。

Whether to use subsystem:console or subsystem:windows kind of depends on whether how you want to start your application:是否使用子系统:控制台或子系统:windows 取决于您是否要启动应用程序:

  • If you use subsystem:console, then you get all of the stdout written to the terminal.如果您使用子系统:控制台,那么您会将所有标准输出写入终端。 The trouble is that if you start the application from the Start Menu/Desktop, you (by default) get a console appearing as well as the application window (which can look pretty ugly).问题是,如果您从开始菜单/桌面启动应用程序,您(默认情况下)会出现一个控制台以及应用程序 window(看起来很丑)。
  • If you use subsystem:windows, you won't get stdout/stderr even if you run the application from a DOS window, Cygwin , or other terminal.如果您使用子系统:windows,即使您从DOS window、 Cygwin或其他终端运行应用程序,您也不会得到 stdout/stderr。

If you want the middle way which is to output to the terminal IF the application was started in a terminal, then follow the link that Luke provided in his solution ( http://dslweb.nwnexus.com/~ast/dload/guicon.htm )如果您想要 output 到终端的中间方式,如果应用程序是在终端中启动的,那么请按照 Luke 在他的解决方案中提供的链接( http://dslweb.nwnexus.com/~ast/dload/guicon。 .htm )

For reference, I ran into this problem with an application that I want to run in either normal Windows mode or batch mode (that is, as part of a script) depending on command-line switches.作为参考,我遇到了这个问题,我想根据命令行开关在正常 Windows 模式或批处理模式(即作为脚本的一部分)下运行该应用程序。 The whole differentiation between console and Windows applications is a bit bizarre to Unix folks!控制台和 Windows 应用程序之间的整个区别对于 Unix 的人来说有点奇怪!

If you're using Visual Studio, it should work just fine!如果您使用的是 Visual Studio,它应该可以正常工作!

Here's a code example:这是一个代码示例:

#include <iostream>

using namespace std;

int main (int) {
    cout << "This will print to the console!" << endl;
}

Make sure you chose a Win32 console application when creating a new project.确保在创建新项目时选择了 Win32 控制台应用程序。 Still you can redirect the output of your project to a file by using the console switch (>>).您仍然可以使用控制台开关 (>>) 将项目的 output 重定向到文件。 This will actually redirect the console pipe away from the stdout to your file.这实际上会将控制台 pipe 从标准输出重定向到您的文件。 (for example, myprog.exe >> myfile.txt ). (例如, myprog.exe >> myfile.txt )。

I wish I'm not mistaken!我希望我没有记错!

Your application must be compiled as a Windows console application.您的应用程序必须编译为 Windows 控制台应用程序。

I assume you're using some version of Visual Studio?我假设您使用的是某个版本的 Visual Studio? In windows, std::cout << "something";在 windows 中, std::cout << "something"; should write something to a console window IF your program is setup in the project settings as a console program.如果您的程序在项目设置中设置为控制台程序,则应该向控制台 window 写入一些内容。

If using MinGW , add an option, -Wl,subsystem,console or -mconsole .如果使用MinGW ,请添加一个选项 -Wl -Wl,subsystem,console-mconsole

You don't necessarily need to make any changes to your code (nor to change the SUBSYSTEM type).您不一定需要对代码进行任何更改(也不需要更改SUBSYSTEM类型)。 If you wish, you also could simply pipe stdout and stderr to a console application (a Windows version of cat works well).如果您愿意,您也可以简单地将 pipe标准输出和标准错误添加到控制台应用程序(Windows 版本的cat运行良好)。

There is a good solution 有一个很好的解决方案

if (AllocConsole() == 0)
{
    // Handle error here. Use ::GetLastError() to get the error.
}

// Redirect CRT standard input, output and error handles to the console window.
FILE * pNewStdout = nullptr;
FILE * pNewStderr = nullptr;
FILE * pNewStdin = nullptr;

::freopen_s(&pNewStdout, "CONOUT$", "w", stdout);
::freopen_s(&pNewStderr, "CONOUT$", "w", stderr);
::freopen_s(&pNewStdin, "CONIN$", "r", stdin);

// Clear the error state for all of the C++ standard streams. Attempting to accessing the streams before they refer
// to a valid target causes the stream to enter an error state. Clearing the error state will fix this problem,
// which seems to occur in newer version of Visual Studio even when the console has not been read from or written
// to yet.
std::cout.clear();
std::cerr.clear();
std::cin.clear();

std::wcout.clear();
std::wcerr.clear();
std::wcin.clear();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM