Namespaces
Variants
Views
Actions

std::system

From cppreference.com
< cpp‎ | utility‎ | program
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
(C++26)
Three-way comparison
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)

 
 
Defined in header <cstdlib>
int system( const char* command );

Calls the host environment's command processor (e.g. /bin/sh, cmd.exe) with the parameter command. Returns an implementation-defined value (usually the value that the invoked program returns).

If command is a null pointer, checks if the host environment has a command processor and returns a nonzero value if and only if the command processor exists.

Contents

[edit] Parameters

command - character string identifying the command to be run in the command processor. If a null pointer is given, command processor is checked for existence

[edit] Return value

Implementation-defined value. If command is a null pointer, returns a nonzero value if and only if the command processor exists.

[edit] Notes

On POSIX systems, the return value can be decomposed using WEXITSTATUS and WSTOPSIG.

The related POSIX function popen makes the output generated by command available to the caller.

An explicit flush of std::cout is also necessary before a call to std::system, if the spawned process performs any screen I/O.

[edit] Example

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

Possible output:

total 16
-rwxr-xr-x 1 2001 2000 8859 Sep 30 20:52 a.out
-rw-rw-rw- 1 2001 2000  161 Sep 30 20:52 main.cpp
-rw-r--r-- 1 2001 2000    0 Sep 30 20:52 test.txt

[edit] See also

C documentation for system