std::system

Header: <cstdlib>

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).

# Declarations

int system( const char* command );

# Parameters

# Return value

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

# 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.

# 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();
}

# See also