为什么我的程序的输出在 Windows 中闪烁并关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1048975/
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-09-15 12:43:06  来源:igfitidea点击:

Why does my program's output flash and close in Windows?

cwindowsconsole

提问by JDelage

I'm trying to build an .exe file for the K&R "Hello, world". The code given in the book is:

我正在尝试为 K&R“Hello, world”构建一个 .exe 文件。书中给出的代码是:

#include <stdio.h>  
main()  
{  
    printf("Hello, world!\n");  
}  

When I build & run from Code::Blocks (under Windows XP), I get the prompt window with the "hello world" message. It stays open until I close it manually. However, when I double click the .exe file, the prompt just flashes and disappears, why is that?

当我从 Code::Blocks(在 Windows XP 下)构建和运行时,我会看到带有“hello world”消息的提示窗口。它保持打开状态,直到我手动关闭它。但是,当我双击.exe文件时,提示只是闪烁并消失,这是为什么?

回答by rix0rrr

No one is explicitly telling you this, so I will:

没有人明确告诉你这一点,所以我会:

What you see when you double-click the file is normal. What your IDE does (keeping the window open) is a featureto help you debug the application.

双击文件时看到的是正常的。您的IDE做什么(保持窗口打开)是一个功能,以帮助您调试应用程序。

Why is this so?

为什么会这样?

Since you're developing a consoleapplication, there has to be a console for your application to display its output on. If there is none yet, a new console is created (which is the black window).

由于您正在开发控制台应用程序,因此您的应用程序必须有一个控制台来显示其输出。如果还没有,则会创建一个新控制台(即黑色窗口)。

If you launch your program from inside a console (say, from cmd.exe), it will just inherit the console of the parent without creating a new one[1].

如果您从控制台内部(例如 from cmd.exe)启动您的程序,它只会继承父级的控制台,而不会创建新的控制台 [1]。

After the last application using the console exits (which, in the first case is just your program), the console closes. You will notice this all the time for console applications that print nothing but a help text when run without parameters. If you double-click them from explorer, a black window with some text will flash and then immediately close.

在使用控制台的最后一个应用程序退出后(在第一种情况下只是您的程序),控制台将关闭。对于在没有参数的情况下运行时只打印帮助文本的控制台应用程序,您会一直注意到这一点。如果您从资源管理器中双击它们,一个带有一些文本的黑色窗口将闪烁,然后立即关闭。

  • Sometimes, a program that does something and them immediately closes is what you want. For example, you can call these applications from scripts.

  • On the other hand, your application could be interactive: waiting for user input, doing some thing, and only exiting when the user tells it to. You cannot script these applications, obviously, as you will need to have a human present at the keyboard to tell the application what to do.

  • 有时,您想要一个执行某些操作并立即关闭的程序。例如,您可以从脚本调用这些应用程序。

  • 另一方面,你的应用程序可以是交互式的:等待用户输入,做一些事情,并且只有在用户告诉它时才退出。显然,您不能编写这些应用程序的脚本,因为您需要有人在键盘前告诉应用程序该做什么。

Now we get to the IDE part: let's say you're developing an application of the first kind, one that does something and then immediately closes. It's not very convenient to have the screen flash and disappear every time you run it, because how can you tell if the program worked? Assuming you can tell this from the output it generates.

现在我们进入 IDE 部分:假设您正在开发第一种应用程序,该应用程序执行某些操作然后立即关闭。每次运行都让屏幕闪烁和消失不是很方便,因为你怎么知道程序是否有效?假设您可以从它生成的输出中看出这一点。

You could of course start a command-line window and run the application from there, but the program would execute separately from the IDE, and you would lose live debugging capabilities.

您当然可以启动命令行窗口并从那里运行应用程序,但该程序将与 IDE 分开执行,并且您将失去实时调试功能。

So, IDE makers came up with a feature for console applications: when you run the application directly from your IDE, they afterwards, usually waiting for a keypress. This gives you the opportunity to inspect the window with the output, to confirm that the application is working properly.

因此,IDE 制造商为控制台应用程序提出了一项功能:当您直接从 IDE 运行应用程序时,他们通常会等待按键操作。这使您有机会检查带有输出的窗口,以确认应用程序是否正常工作。



[1] Esoterica: unless you go through an application that does not inherit the console. Any console app launched by that application will not inherit the console, since the inheritance was broken by the GUI app. For example, start.exedoes this. Compare:

[1] Esoterica:除非你通过一个不继承控制台的应用程序。该应用程序启动的任何控制台应用程序都不会继承控制台,因为 GUI 应用程序破坏了继承。例如,start.exe这样做。相比:

foo.exe (inherits the console)
start foo.exe (start.exe is a GUI app, so foo.exe is launched in a new console)

回答by drewh

If you're not running a command line exe from an already open command line window, Windows will automatically close the windows after the program has terminated. Try opening cmd.exe, navigating to your program's directory and running it from there, the window should stay open.

如果您不是从已经打开的命令行窗口运行命令行 exe,Windows 将在程序终止后自动关闭这些窗口。尝试打开 cmd.exe,导航到您的程序目录并从那里运行它,窗口应该保持打开状态。

回答by ólafur Waage

When running from IDE's like this, they run the program and when its done running, they close it.

当像这样从 IDE 运行时,他们运行程序,当它运行完成时,他们关闭它。

Since your program's only function is to print out a value, it does that and closes.

由于您的程序的唯一功能是打印出一个值,因此它会执行此操作并关闭。

You should try to add something that asks for user input at the end or compile it into an .exe and run itself from the command line yourself.

您应该尝试在最后添加一些要求用户输入的内容,或者将其编译为 .exe 并自己从命令行运行。

Since you are starting I would recommend to just run it from the command line yourself. You will eventually learn about user input and there you can have the command line window open when you use your program.

由于您刚开始,我建议您自己从命令行运行它。您最终将了解用户输入,并在那里您可以在使用程序时打开命令行窗口。

回答by Ksempac

Normal behavior.

正常行为。

Your program executes every action in the order of the main. So it prints, and then moves on to the next operation, there is none, so it exits. Since the console window is tied to your .exe, the command window closes with the program.

你的程序按照主程序的顺序执行每一个动作。所以它打印,然后继续下一个操作,没有,所以它退出。由于控制台窗口与您的 .exe 相关联,因此命令窗口将随程序关闭。

If you don't want your program to exit right away, you can make it sleep, or wait for user input before exiting.

如果您不希望您的程序立即退出,您可以让它休眠,或者在退出前等待用户输入。

回答by JaredPar

When double clicking a .exe in Windows, you are launching a new process. Windows has 2 basic process types: Window and Command line. The hello world sample you've written is a command line process.

在 Windows 中双击 .exe 时,您正在启动一个新进程。Windows 有 2 种基本进程类型:窗口和命令行。您编写的 hello world 示例是一个命令行进程。

A command line process will launch a new command window on startup. This is the window that pops up which is largely a black background with white text. Upon completion of a program the window will close down.

命令行进程将在启动时启动一个新的命令窗口。这是弹出的窗口,主要是黑色背景和白色文本。程序完成后,窗口将关闭。

回答by Christy John

Add getch();before the closing brace. This will prompt for an input after the output is printed. Once you key in a character the window will close. This should solve your problem.

getch();在右大括号之前添加。这将在打印输出后提示输入。一旦你输入一个字符,窗口就会关闭。这应该可以解决您的问题。

回答by nstehr

That is because from the executable, it executes your code in a new window and then the process is done, it has no reason to stay open, what you wanted to do is complete. There are a couple of things you can do. You can execute it from the cmd.exe command line, or you could even put something at the end of your code that listens for a key press, and once the key stroke is detected, allow the program to exit.

那是因为从可执行文件中,它会在新窗口中执行您的代码,然后该过程完成,它没有理由保持打开状态,您想要做的已经完成。您可以做几件事。您可以从 cmd.exe 命令行执行它,或者您甚至可以在代码的末尾放置一些东西来侦听按键,一旦检测到按键,就允许程序退出。

回答by Thomas Owens

The preferred solution is to run the executable from the command line.

首选的解决方案是从命令行运行可执行文件。

回答by hillu

Try running your binary from the command line.

尝试从命令行运行二进制文件。

回答by amischiefr

That is because the executable file opens its own dialog box. When the executable has completed running it shuts down the dialog box that it opened in order to run. However, when YOU are the one that opened the dialog box, it disappears when YOU close it.

这是因为可执行文件会打开自己的对话框。当可执行文件运行完毕后,它会关闭为运行而打开的对话框。但是,当您是打开对话框的人时,当您关闭它时它就会消失。

So if you were to open up a command prompt and then run the executable, the dialog box would not automatically close.

因此,如果您要打开命令提示符然后运行可执行文件,该对话框将不会自动关闭。