在C ++中使用system(“ pause”)命令

In this article, we’ll take a look at using the system(“pause”) command in C++.

在本文中,我们将介绍如何在C ++中使用system(“ pause”)命令。

Before going through this article, note this the system("pause") command is only available in Windows Systems.

在阅读本文之前,请注意,此system("pause")命令在Windows系统中可用。

This means that you cannot use this from any Linux / Mac machine.

这意味着您不能在任何Linux / Mac计算机上使用它。



system()命令 (The system() command)

Before going through the system(“pause”) command, let’s understand what system() does.

在执行system(“ pause”)命令之前,让我们了解一下system()作用。


#include <cstdlib>

int system(const char *command);

The system() function performs a call to the Operating System to run a particular command.

system()函数执行对操作系统的调用以运行特定命令。

Note that we must include the <cstdlib> header file.

注意,我们必须包括<cstdlib>头文件。

This is very similar to opening a terminal and executing that command by hand.

这与打开终端并手动执行该命令非常相似。

For example, if you want to use the “ls” command from Linux, you can use system("ls").

例如,如果要在Linux中使用“ ls”命令,则可以使用system("ls")

If you are having any Linux/Mac machine, you can try the below code.

如果您有任何Linux / Mac计算机,则可以尝试以下代码。


#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
    // Try the "ls -l" command from your Linux / Mac machine
    int ret = system("ls -l > test.txt");
    return 0;
}

Possible Output

可能的输出


total 16
-rwxr-xr-x 1 2001 2000 9712 Jun 25 21:11 a.out
-rw-rw-rw- 1 2001 2000  209 Jun 25 21:11 main.cpp
-rw-r--r-- 1 2001 2000    0 Jun 25 21:11 test.txt

Now that we’re a bit clear on what system() can do, let’s look at the system(“pause”) command.

现在我们对system()可以做什么有了一个清晰的认识,让我们看一下system(“ pause”)命令。



在C ++中使用system(“ pause”)命令 (Using system(“pause”) command in C++)

This is a Windows-specific command, which tells the OS to run the pause program.

这是Windows特定的命令,它告诉OS运行pause程序。

This program waits to be terminated, and halts the exceution of the parent C++ program. Only after the pause program is terminated, will the original program continue.

该程序等待终止,并停止执行父C ++程序。 仅在暂停程序终止后,原始程序才会继续。

If you’re using a Windows machine, you can run the below code:

如果您使用的是Windows计算机,则可以运行以下代码:


#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
    for (int i=0; i<10; i++) {
        cout << "i = " << i << endl;
        if (i == 5) {
            // Call the pause command
            cout << "Calling the pause command\n";
            system("pause");
            cout << "pause program terminated. Resuming...\n";
        }
    }
    return 0;
}

Output – From Windows System

输出–从Windows系统


i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
Calling the pause command
Press any key to continue . . .
pause program terminated. Resuming...
i = 6
i = 7
i = 8
i = 9

E:\Programs\sample.exe (process 14052) exited with code 0.

As you can observe, the pause command was indeed executed when our if condition i = 5.

如您所见,当if条件i = 5时,确实执行了stop命令。

After we hit enter, we terminated the pause program, and resumed our loop in C++ program!

按下回车键后,我们终止了暂停程序,并在C ++程序中恢复了循环

使用system(“ pause”)命令的缺点 (Disadvantages of using the system(“pause”) command)

The main pitfall of system(“pause”) is that this is platform specific. This does not work on Linux/Mac systems, and is not portable.

系统的主要陷阱(“暂停”)是特定于平台的。 这在Linux / Mac系统上不起作用,并且不可移植。

While this works as a kind of a hack for Windows systems, this approach may easily cause errors, when you try to run the code on other systems!

尽管这对于Windows系统是一种黑客手段,但是当您尝试在其他系统上运行代码时,这种方法很容易导致错误!

Therefore, I would suggest some other alternative ways to pause and resume a program, such as using signal handlers.

因此,我建议使用其他替代方法来暂停和恢复程序,例如使用信号处理程序。



结论 (Conclusion)

In this article, we learned how we could use the system(“pause”) command in C++. For similar content, do go through our tutorial section on C++ programming!

在本文中,我们学习了如何在C ++中使用system(“ pause”)命令。 对于类似的内容,请阅读我们有关C ++编程的教程部分

参考资料 (References)



翻译自: https://www.journaldev.com/41740/system-pause-command-c-plus-plus

  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值