In this article, we’ll take a look at using the system(“pause”) command in C++.
Before going through this article, note this the system("pause")
command is only available in Windows Systems.
This means that you cannot use this from any Linux / Mac machine.
Before going through the system(“pause”) command, let’s understand what system()
does.
#include <cstdlib>
**int**` `**system**``(``**const**` `**char**` `*command);
The system()
function performs a call to the Operating System to run a particular command.
Note that we must include the <cstdlib>
header file.
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")
.
If you are having any Linux/Mac machine, you can try the below code.
#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.
#programming-c #cplusplus