Namespaces
Variants

cpp/utility/program/system: Difference between revisions

From cppreference.com
remove command.com archaic
Space Mission (talk | contribs)
m fmt `popen`
 
Line 1: Line 1:
{{cpp/title| system}}
{{cpp/title|system}}
{{cpp/utility/program/navbar}}
{{cpp/utility/program/navbar}}
{{ddcl | header=cstdlib |
{{ddcl|header=cstdlib|
int system( const char* command );
int system( const char* command );
}}
}}
Line 11: Line 11:
===Parameters===
===Parameters===
{{par begin}}
{{par begin}}
{{par | 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}}
{{par|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}}
{{par end}}
{{par end}}


Line 18: Line 18:


===Notes===
===Notes===
On POSIX systems, the return value can be decomposed using [http://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html WEXITSTATUS and WSTOPSIG]
On POSIX systems, the return value can be decomposed using [://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html WEXITSTATUSand WSTOPSIG]


The related POSIX function [http://pubs.opengroup.org/onlinepubs/9699919799/functions/popen.html popen] makes the output generated by {{tt|command}} available to the caller.
The related POSIX function [://pubs.opengroup.org/onlinepubs/9699919799/functions/popen.html popen] makes the output generated by {{tt|command}} available to the caller.


An explicit flush  of {{lc|std::cout}} is also necessary before a call to {{lc|std::system}}, if the spawned process performs any screen I/O.
An explicit flush  of {{lc|std::cout}} is also necessary before a call to {{lc|std::system}}, if the spawned process performs any screen I/O.
Line 26: Line 26:
===Example===
===Example===
{{example
{{example
|
|
| code=
|code=
#include <cstdlib>
#include <cstdlib>
#include <fstream>
#include <fstream>
Line 34: Line 34:
int main()
int main()
{
{
     std::system("ls -l >test.txt"); // execute the UNIX command "ls -l >test.txt"
     std::system("ls -l >test.txt"); // the UNIX command "ls -l >test.txt"
     std::cout << std::ifstream("test.txt").rdbuf();
     std::cout << std::ifstream("test.txt").rdbuf();
}
}
|p=true
|p=true
| output=
|output=
total 16
total 16
-rwxr-xr-x 1 2001 2000 8859 Sep 30 20:52 a.out
-rwxr-xr-x 1 2001 2000 8859 Sep 30 20:52 a.out
Line 47: Line 47:
===See also===
===See also===
{{dsc begin}}
{{dsc begin}}
{{dsc see c | c/program/system}}
{{dsc see c|c/program/system}}
{{dsc end}}
{{dsc end}}


{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 23:38, 30 March 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
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.

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

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

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

See also

C documentation for system