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 machineint 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 commandcout << "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 = 9E:\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)

  • cppreference.com page on system() in C++C ++中system()上的cppreference.com页面


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

在C ++中使用system(“ pause”)命令相关推荐

  1. c++中的system(pause)的作用

    作用: system就是调用从程序中调用系统命令(和shell命令). system("pause")就是从程序里调用"pause"命令: 而"pau ...

  2. linux运行c语言pause,c++中的system(pause)的作用和含义解析

    简单来说就是暂停的意思,一般在LINUX编程时会用到,等待接收信号,才会重新运行 . 在进行C/C++编程的时候,在运行程序查看输出效果时,会出现窗口闪一下就关闭的情况. 在C语言中一般通过添加get ...

  3. c语言中pause的作用,c++中的system(pause)的作用和含义解析

    简单来说就是暂停的意思,一般在LINUX编程时会用到,等待接收信号,才会重新运行 . 在进行C/C++编程的时候,在运行程序查看输出效果时,会出现窗口闪一下就关闭的情况. 在C语言中一般通过添加get ...

  4. C++中的system(pause);

    在进行Visual Studio 下的C/C++编程的时候,在运行程序查看输出效果时,会出现窗口闪一下就关闭的情况. 在C语言中一般通过添加getchar(); 在C++中一般在main函数中的ret ...

  5. 在VS2022中,system(“pause“); 的暂停有啥用

    如果不添加system("pause"); 结束输出就是显示 但是如果我添加一个system("pause"); 输出结果就是显示这个 然后再随意按键一下 最后 ...

  6. system(“pause“)和return 0

    写C++程序时会看到别人写system("pause"):和return 0: 但我写程序时不写好像没什么影响. system()是调用系统命令, pause:dos系统命令集合中 ...

  7. C++——system“pause”

    C++使用system( "pause ");来暂停黑窗口 在编写的c++程序中,如果是窗口,有时会一闪就消失了,如果不想让其消失,在程序中添加: system("pau ...

  8. c++中system(pause)的作用和含义

    简单来说就是暂停的意思,一般在LINUX编程时会用到,等待接收信号,才会重新运行 . 在进行C/C++编程的时候,在运行程序查看输出效果时,会出现窗口闪一下就关闭的情况. 在C语言中一般通过添加get ...

  9. C++中system(“pause“)的作用

    system()是调用系统命令 pause:暂停命令 有 system("pause")  按两次黑窗口消失 没有 system("pause")  按一次黑窗 ...

最新文章

  1. Robotium中定位Android客户端疑难元素
  2. 飞桨 AI Studio 课程学习 可以成为一名优秀的算法工程师
  3. 使用redis来实现分布式锁
  4. django Exception Value:no such table: cmdb_XXX
  5. 带有帐号密码验证的apche服务器文件下载
  6. SpringMVC @ModelAttribute注解
  7. Python实战之Selenium自动化测试web刷新FW
  8. 为什么源码中很多方法就一行throw new RuntimeException(Stub!)
  9. java pgp加密_GPG(pgp)加解密中文完整教程
  10. ubuntu mysql 升级_Ubuntu 升级mysql 之后的一些问题
  11. CCS安装多版本编译器 Compiler version__更新手动下载、安装方法
  12. sql sever 插入数据
  13. CSS3实现八方向云台控制器器样式
  14. 电子警察的系统结构和功能设计
  15. 数据库练习题(比较基础)
  16. 14. Setting Ta and RTO【设置Ta和RTO】
  17. linux微信卡,在UOS个人版中运行Wine QQ/微信/TIM很慢,很卡的处理
  18. sqlserver数据库清理(收缩文件)
  19. 微信公众号开发之获取用户列表和用户基本信息(五)
  20. Java 实现归并排序

热门文章

  1. 浪潮超融合服务器虚拟机管理,浪潮在美发布InCloudRail超融合,这是什么样的节奏?...
  2. 阿里CTO程立:阿里巴巴开源的历程、理念和实践
  3. c语言数组所含字节数,(C语言)数组所占字节怎么算?
  4. 吉他谱软件guitar pro2023吉他和弦、六线谱、BASS四线谱绘制
  5. GoF设计模式之代理设计模式
  6. 【中国大学生计算机设计大赛】国二省一备赛心得经验分享
  7. Yii2 事件--自定义事件和系统事件
  8. Oracle EBS Interface/API(31) -PR最终关闭API
  9. 遇到Project Target Framework Not Installed解决方法
  10. java阿里云短信实现