如何在 C++/Windows 中输出到控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/587767/
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
How to output to the console in C++/Windows
提问by Xunil
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?
在 Linux 上使用 C++ 中的 iostream 时,它会在终端中显示程序输出,但在 Windows 中,它只是将输出保存到 stdout.txt 文件中。在 Windows 中,如何使输出显示在控制台中?
采纳答案by MSN
Since you mentioned stdout.txt I google'd it to see what exactly would create a stdout.txt; normally, even with a Windows app, console output goes to the allocated console, or nowhere if one is not allocated.
既然你提到了 stdout.txt,我就用谷歌搜索了一下,看看究竟是什么会创建一个 stdout.txt;通常,即使使用 Windows 应用程序,控制台输出也会进入分配的控制台,如果未分配,则无处可去。
So, assuming you are using SDL (which is the only thing that brought up stdout.txt), you should follow the advice here. Either freopen stdout and stderr with "CON", or do the other linker/compile workarounds there.
因此,假设您正在使用 SDL(这是唯一能够显示 stdout.txt 的内容),您应该遵循此处的建议。要么使用“CON” freopen stdout 和 stderr,要么在那里执行其他链接器/编译解决方法。
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?
"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)."
"If you define "NO_STDIO_REDIRECT" and recompile SDL, I think it will fix the problem." > > (Answer courtesy of Bill Kendrick)
如何避免创建 stdout.txt 和 stderr.txt?
“我相信在 SDL 附带的 Visual C++ 项目中,有一个 SDL_nostdio 目标 > 你可以构建它来做你想要的(TM)。”
“如果您定义“NO_STDIO_REDIRECT”并重新编译 SDL,我认为它会解决问题。” > > (答案由比尔·肯德里克提供)
回答by Sean
For debugging in Visual Studio you can print to the debug console:
要在 Visual Studio 中进行调试,您可以打印到调试控制台:
OutputDebugStringW(L"My output string.");
回答by Sean
If you have a none-console Windows application, you can create a console with the AllocConsolefunction. Once created, you can write to it using the normal std::cout methods.
如果您有一个非控制台的 Windows 应用程序,您可以使用AllocConsole函数创建一个控制台。创建后,您可以使用普通的 std::cout 方法对其进行写入。
回答by Brian R. Bondy
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;
}
回答by jeffm
The AllocConsoleWindows API function will create a console window for your application.
该AllocConsoleWindows API函数会为应用程序创建一个控制台窗口。
回答by AJ.
Whether to use subsystem:console or subsystem:windows kind of depends on whether how you want to start your application:
是使用subsystem:console 还是subsystem: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).
- If you use subsystem:windows, you won't get stdout/stderr even if you run the application from a DOSwindow, Cygwin, or other terminal.
- 如果您使用subsystem:console,那么您会将所有标准输出写入终端。问题在于,如果您从“开始”菜单/桌面启动应用程序,您(默认情况下)会出现一个控制台以及应用程序窗口(看起来很丑陋)。
- 如果您使用子系统:windows,即使您从DOS窗口、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)
如果您想要中间方式,如果应用程序是在终端中启动的,则输出到终端,然后按照 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. The whole differentiation between console and Windows applications is a bit bizarre to Unix folks!
作为参考,我遇到了这个问题,我想根据命令行开关以普通 Windows 模式或批处理模式(即,作为脚本的一部分)运行的应用程序。控制台和 Windows 应用程序之间的整个区别对于 Unix 人员来说有点奇怪!
回答by Galilyou
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. Still you can redirect the output of your project to a file by using the console switch (>>). This will actually redirect the console pipe away from the stdout to your file. (for example, myprog.exe >> myfile.txt
).
确保在创建新项目时选择了 Win32 控制台应用程序。您仍然可以使用控制台开关 (>>) 将项目的输出重定向到文件。这实际上会将控制台管道从标准输出重定向到您的文件。(例如,myprog.exe >> myfile.txt
)。
I wish I'm not mistaken!
我希望我没有记错!
回答by crashmstr
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,则需要制作一个控制台应用程序项目来获取控制台输出。
Second,
第二,
std::cout << "Hello World" << std::endl;
std::cout << "Hello World" << std::endl;
should work in any C++ console application.
应该在任何 C++ 控制台应用程序中工作。
回答by spoulson
Your application must be compiled as a Windows console application.
您的应用程序必须编译为 Windows 控制台应用程序。
回答by Rob K
I assume you're using some version of Visual Studio? In windows, std::cout << "something";
should write something to a console window IF your program is setup in the project settings as a console program.
我假设您使用的是某些版本的 Visual Studio?在 Windows 中,std::cout << "something";
如果您的程序在项目设置中设置为控制台程序,则应该向控制台窗口写入一些内容。