C++ 系统(“暂停”);- 为什么错了?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1107705/
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
system("pause"); - Why is it wrong?
提问by Faken
Here's a question that I don't quite understand:
这里有一个我不太明白的问题:
The command, system("pause");
is taught to new programmers as a way to pause a program and wait for a keyboard input to continue. However, it seems to be frowned on by many veteran programmers as something that should not be done in varying degrees.
该命令system("pause");
被教授给新程序员,作为暂停程序并等待键盘输入继续的一种方式。但是,对于很多资深程序员来说,这似乎是不同程度的不应该做的事情。
Some people say it is fine to use. Some say it is only to be used when you are locked in your room and no one is watching. Some say that they will personally come to your house and kill you if you use it.
有人说用着就好。有人说它只在你被锁在房间里并且没有人在看的时候使用。有人说,如果你使用它,他们会亲自来到你家并杀死你。
I, myself am a new programmer with no formal programming training. I use it because I was taught to use it. What I don't understand is that if it is not something to be used, then why was I taught to use it? Or, on the flip side, is it really not that bad after all?
我,我自己是一个没有正式编程培训的新程序员。我使用它是因为我被教导使用它。我不明白的是,如果它不是可以使用的东西,那为什么要教我使用它?或者,另一方面,它真的没有那么糟糕吗?
What are your thoughts on this subject?
你对这个问题有什么想法?
采纳答案by ravuya
It's frowned upon because it's a platform-specific hack that has nothing to do with actually learning programming, but instead to get around a feature of the IDE/OS - the console window launched from Visual Studio closes when the program has finished execution, and so the new user doesn't get to see the output of his new program.
它不受欢迎,因为它是一个特定于平台的 hack,与实际学习编程无关,而是绕过 IDE/OS 的一个功能 - 从 Visual Studio 启动的控制台窗口在程序完成执行时关闭,因此新用户无法看到他的新程序的输出。
Bodging in System("pause") runs the Windows command-line "pause" program and waits for that to terminate before it continues execution of the program - the console window stays open so you can read the output.
Bodging in System("pause") 运行 Windows 命令行“暂停”程序并等待该程序终止,然后再继续执行该程序 - 控制台窗口保持打开状态,以便您可以读取输出。
A better idea would be to put a breakpoint at the end and debug it, but that again has problems.
一个更好的主意是在最后放置一个断点并对其进行调试,但这又出现了问题。
回答by ravuya
It's slow. It's platform dependent. It's insecure.
它很慢。它依赖于平台。这是不安全的。
First: What it does. Calling "system" is literally like typing a command into the windows command prompt. There is a ton of setup and teardown for your application to make such a call - and the overhead is simply ridiculous.
第一:它做什么。调用“系统”实际上就像在 Windows 命令提示符中键入命令一样。要进行这样的调用,您的应用程序需要进行大量的设置和拆卸工作——而且开销简直是荒谬的。
What if a program called "pause" was placed into the user's PATH? Just calling system("pause") only guarantees that a program called "pause" is executed (hope that you don't have your executable named "pause"!)
如果将名为“pause”的程序放入用户的 PATH 中会怎样?仅调用 system("pause") 只能保证执行名为 "pause" 的程序(希望您没有名为 "pause" 的可执行文件!)
Simply write your own "Pause()" function that uses _getch. OK, sure, _getch is platform dependent as well (note: it's defined in "conio.h") - but it's much nicer than system()
if you are developing on Windows and it has the same effect (though it is your responsibility to provide the text with cout or so).
只需编写您自己的使用 _getch 的“Pause()”函数。好的,当然,_getch 也依赖于平台(注意:它在“conio.h”中定义)-但它比system()
在 Windows 上开发要好得多,并且具有相同的效果(尽管提供文本是您的责任)与 cout 左右)。
Basically: why introduce so many potential problems when you can simply add two lines of code and one include and get a much more flexible mechanism?
基本上:当您可以简单地添加两行代码和一个包含并获得更灵活的机制时,为什么会引入这么多潜在问题?
回答by Gavin H
- slow: it has to jump through lots of unnecessary Windows code and a separate program for a simple operation
- not portable: dependent on the pause program
- not good style: making a System call should only be done when reallynecessary
- more typing: System("pause") is longer than getchar()
- 慢:它必须跳过许多不必要的 Windows 代码和一个单独的程序才能进行简单的操作
- 不可移植:取决于暂停程序
- 不好的风格:只有在真正需要时才应该进行系统调用
- 更多类型: System("pause") 比 getchar() 长
a simple getchar() should do just fine.
一个简单的 getchar() 应该就可以了。
回答by Cheers and hth. - Alf
Using system("pause");
is Ungood Practice™ because
使用system("pause");
是 Ungood Practice™ 因为
It's completely unnecessary.
To keep the program's console window open at the end when you run it from Visual Studio, use Ctrl+F5to run it without debugging, or else place a breakpoint at the last right brace}
ofmain
. So, no problem in Visual Studio. And of course no problem at all when you run it from the command line.It's problematic & annoying
when you run the program from the command line. For interactive execution you have to press a key at the end to no purpose whatsoever. And for use in automation of some task thatpause
is very much undesired!It's not portable.
Unix-land has no standardpause
command.
这是完全没有必要的。
为了保持程序的控制台窗口打开,在结束的时候,你从Visual Studio中,使用运行Ctrl+F5无需调试运行,否则发生在最后的右括号断点}
的main
。因此,在 Visual Studio 中没有问题。当然,从命令行运行它时完全没有问题。
当您从命令行运行程序时,这是有问题且烦人的。对于交互式执行,您必须在最后无任何目的地按下一个键。并用于一些pause
非常不受欢迎的任务的自动化!它不便携。
Unix-land 没有标准pause
命令。
The pause
command is an internal cmd.exe
command and can't be overridden, as is erroneously claimed in at least one other answer. I.e. it's not a security risk, and the claim that AV programs diagnose it as such is as dubious as the claim of overriding the command (after all, a C++ program invoking system
is in position to do itself all that the command interpreter can do, and more). Also, while this way of pausing is extremely inefficient by the usual standards of C++ programming, that doesn't matter at all at the end of a novice's program.
该pause
命令是一个内部cmd.exe
命令,不能被覆盖,正如至少在另一个答案中错误地声称的那样。即它不是安全风险,并且声称 AV 程序将其诊断为这样的说法与覆盖命令的说法一样可疑(毕竟,调用 C++ 程序system
可以自行完成命令解释器可以做的所有事情,并且更多的)。此外,虽然按照 C++ 编程的通常标准,这种暂停方式效率极低,但在新手程序结束时,这根本无关紧要。
So, the claims in the horde of answers before this are not correct, and the main reason you shouldn't use system("pause")
or any other wait commandat the end of your main
, is the first point above: it's completely unnecessary, it serves absolutely no purpose, it's just very silly.
所以,在答案的部落的权利要求在此之前是不正确的,你不应该使用的主要理由system("pause")
或任何其他等待命令在你的结束main
,是第一点之上:它是完全不必要的,它提供绝对没有目的,简直太傻了。
回答by John T
In summary, it has to pause the programs execution and make a system call and allocate unnecessary resources when you could be using something as simple as cin.get(). People use System("PAUSE") because they want the program to wait until they hit enter to they can see their output. If you want a program to wait for input, there are built in functions for that which are also cross platform and less demanding.
总之,当您可以使用像 cin.get() 这样简单的东西时,它必须暂停程序执行并进行系统调用并分配不必要的资源。人们使用 System("PAUSE") 是因为他们希望程序等到他们按下回车键才能看到他们的输出。如果您希望程序等待输入,则可以使用内置函数,这些函数也是跨平台且要求不高的。
Further explanation in thisarticle.
在进一步的解释这文章。
回答by Gavriel Feria
You can use std::cin.get()
from iostream
:
您可以使用std::cin.get()
从iostream
:
#include <iostream> // std::cout, std::cin
using namespace std;
int main() {
do {
cout << '\n' << "Press the Enter key to continue.";
} while (cin.get() != '\n');
return 0;
}
Besides, system('pause')
is slow, and includes a file you probably don't need: stdlib.h
. It is platform-dependent, and actually calls up a 'virtual' OS.
此外,system('pause')
速度很慢,并且包含一个您可能不需要的文件:stdlib.h
. 它依赖于平台,实际上调用了一个“虚拟”操作系统。
回答by a_m0d
Because it is not portable.
因为它不便携。
pause
is a windows / dos only program, so this your code won't run on linux. Moreover, system
is not generally regarded as a very good way to call another program - it is usually better to use CreateProcess
or fork
or something similar.
是一个仅适用于 windows/dos 的程序,因此您的代码不会在 linux 上运行。而且,system
通常不被认为是调用另一个程序的一种很好的方式——通常使用CreateProcess
或fork
或类似的东西更好。
回答by Sam Harwell
As listed on the other answers, there are many reasons you can find to avoid this. It all boils down to one reason that makes the rest moot. The System()
function is inherently insecure/untrusted, and should not be introduced into a program unless necessary.
正如其他答案中列出的那样,您可以找到很多原因来避免这种情况。这一切都归结为一个使其余部分毫无意义的原因。该System()
函数本质上是不安全/不受信任的,除非必要,否则不应将其引入程序。
For a student assignment, this condition was never met, and for this reason I would fail an assignment without even running the program if a call to this method was present. (This was made clear from the start.)
对于学生作业,从来没有满足过这个条件,因此,如果存在对该方法的调用,我什至会在不运行程序的情况下完成作业。(这一点从一开始就明确了。)
回答by Sebastian Mach
For me it doesn't make sense in general to wait before exiting without reason. A program that has done its work should just end and hand over its resources back to its creator.
对我来说,在无故退出之前等待通常没有意义。完成其工作的程序应该结束并将其资源交还给其创建者。
One also doesn't silently wait in a dark corner after a work day, waiting for someone tipping ones shoulder.
也不会在工作了一天后,默默地等待在黑暗的角落里,等着有人搭讪。
回答by Sepideh Abadpour
system("pause");
is wrong because it's part of Windows API and so it won't work in other operation systems.
是错误的,因为它是 Windows API 的一部分,因此它在其他操作系统中不起作用。
You should try to use just objects from C++ standard library. A better solution will be to write:
您应该尝试仅使用 C++ 标准库中的对象。更好的解决方案是编写:
cin.get();
return 0;
But it will also cause problems if you have other cin
s in your code. Because after each cin
, you'll tap an Enter
or \n
which is a white space character. cin
ignores this character and leaves it in the buffer zone but cin.get()
, gets this remained character. So the control of the program reaches the line return 0
and the console gets closed before letting you see the results.
To solve this, we write the code as follows:
但是如果您cin
的代码中有其他s,它也会导致问题。因为在每个 之后cin
,您将点击一个Enter
或\n
它是一个空白字符。cin
忽略此字符并将其留在缓冲区中,但是cin.get()
,获取此剩余字符。因此,程序的控制到达线路return 0
并且控制台在让您看到结果之前关闭。
为了解决这个问题,我们编写如下代码:
cin.ignore();
cin.get();
return 0;