C++ Shell Or System Programming Tutorial With Examples

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

This tutorial gives a detailed account of the C++ Shell or system () call that is used to invoke the operating system command from a C++ program.

In the software programming world, most of the operating system APIs are targeted at C. C++ language provides direct support for calling C functions from the C++ code.

Hence, in this case, C++ also becomes a system programming language. C++ provides a “system ()” command to invoke the operating system commands from the C/C++ program.

In other words, we can say that the system () command executes a C++ shell command. In this tutorial, we will discuss the execution of the shell command or system () in detail.

=> Explore The Simple C++ Training Series Here.

C++ Shell (System Programming)

C++ System Calls

Now let’s discuss the System call and its details with examples.

Function Prototype: int system (const char* command);

Parameters:

command=> A C-string containing the command to be executed.

If a null pointer is passed, then only a check for the command processor is done.

Return Value: Returns an int value. The value returned depends on whether the C-style string is specified as an argument or if a null pointer is specified.

  • Null Pointer => If the null pointer is specified, then it returns a non-zero value if the command processor is available and zero otherwise.
  • The command specified => When the command is specified, then the status code is usually returned but the value returned depends on the system and library implementation.

Description: The system command executes a command supplied as an argument. The value returned by executing the command is usually system and library implementation-dependent. If a null pointer is passed instead of a command, then this call simply checks if the command processor is available or not.

The call returns a non-zero value if the command processor is available and zero otherwise.

Using system (), we can run almost any command provided the operating system allows it. For example, we can run the system (“dir”) or system (“ls”) with equal ease. In fact, we can even invoke the GCC compiler from our program.

Enlisted below are a few examples of system commands that are used in C++ to execute the C++ shell commands.

Example 1:

This example shows the system command demonstration with a null pointer as an argument.

#include <iostream> 
#include <cstdlib>  
using namespace std;

int main ()
{
  int i;
  cout<< "Check if command processor is available..."<<endl;
  if (system(NULL)) cout << "command processor is available!!"<<endl;
    else exit (EXIT_FAILURE);
  cout<< "Executing command DIR..."<<endl;
  i=system ("dir");
  cout << "The value returned was:"<<i<<endl;
  return 0;
}

Output:

output - command processor

In the above program, we first check if the command processor is available by passing null to the system call. If the command processor is available then we execute the dir command. If the command processor is not available then we exit the program with a failure.

Example 2:

The below example shows the execution of the ls command wherein the output is piped to a text file “output.txt”. After the system () call is executed, we print the contents of the output.txt.

#include <cstdlib>
#include <fstream>
#include <iostream>
 
int main()
{
    std::system("ls -l >output.txt"); // execute the UNIX command "ls -l >test.txt"
    std::cout << std::ifstream("output.txt").rdbuf();
}

Output:

output - execution of ls command

The output of the above program is the contents of the file “output.txt” which is nothing but the output of the ls command.

Example 3:

The C++ program below is the continuation of the previous example. Here we execute the ls command that is redirected to output.txt using a system call. Then we execute another system call with the “rm” (remove) command to remove file output.txt.

After this, we again execute the ls command, and this time we redirect the output to another file i.e. text.txt. Finally, we print the contents of the text.txt file.

#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    // execute the UNIX command "ls -l >output.txt"
    
system("ls -l >output.txt"); 
cout << ifstream("output.txt").rdbuf();

    // execute the UNIX command "rm output.txt"
system("rm output.txt"); 
cout<<"removed output.txt"<<endl;
    
    // execute the UNIX command "ls -l >text.txt"
cout<<"ls after removing output.txt & creating text.txt"<<endl;
system("ls -l >text.txt"); 
cout << ifstream("text.txt").rdbuf();
}

Output:

output - Redirect output to another file text.txt

C++ System Pause

The system (“pause”) command temporarily halts the operations when executed. The system (“pause”) call is Operating system dependent and performs the following steps:

  • This call suspends the program temporarily and also signals the operating system to open the operating system shell.
  • The operating system allocates the memory for the command to execute.
  • Then it deallocates the memory, exits the operating system, and resumes the suspended program.

The following program shows an example of a system (“pause”) call.

#include <iostream>
#include <cstdlib>

using namespace std; 
int main () 
{ 
    cout << "Hello World!" << endl; 
    system("pause"); 
    return 0; 
} 

Output:

output - Pause call

As already mentioned, the system (“pause”) call is very slow and is operating system dependent. The steps mentioned above are heavy to execute.

Additionally, the system calls may also pose some security risks. Hence we usually do not rely on the system (“pause”) calls in our programs.

Instead, we can use cin.get to achieve the same functionality as a system (“pause”) as shown in the below program.

#include <iostream> 
#include <cstdlib> 
using namespace std; 
int main () 
{ 
    cout << "This is SoftwareTestingHelp.com" << endl; 
    cin.get();  // same as getchar() 
    return 0; 
}

Output:

output - usage of cin.get

As shown above, we can use cin.get to pause the output until we press some key. Unlike the system (“pause”) is not operating system dependent. It also does not follow the steps carried out when we execute the system (“pause”).

System Vs Library Functions

The system calls are operating system dependent. They are also very slow and heavy on resources. Library functions are not OS-dependent. They are faster and do not consume too many resources or memory.

The most common uses of system calls are for system (“pause”) and system (“cls”) commands. Library functions are built-in functions that contain functions related to math, file I/O, etc.

Conclusion

In this C++ Shell tutorial, we discussed various system functions. We saw examples of passing a null pointer to system command that checks if the command processor is available or not. We also discussed the system (“pause”) command and its alternatives in detail.

=> Check ALL C++ Tutorials Here.

Was this helpful?

Thanks for your feedback!

Leave a Comment