Namespaces
Variants

cpp/utility/program/system: Difference between revisions

From cppreference.com
+popen in notes
m s/NULL/null, and made the example output that file it created
Line 2: Line 2:
{{cpp/utility/program/navbar}}
{{cpp/utility/program/navbar}}
{{ddcl | header=cstdlib |
{{ddcl | header=cstdlib |
int system( const char *command );
int system( const char* command );
}}
}}


Calls the host environment's command processor with command parameter. Returns implementation-defined value (usually the value that the invoked program returns).
Calls the host environment's command processor with command. Returns implementation-defined value (usually the value that the invoked program returns).


If command is the {{lc|NULL}} pointer, checks if host environment has a command processor and returns nonzero value only if the command processor exists.
If {{|}} pointer, checks if host environment has a command processor and returns nonzero value only if the command processor exists.


===Parameters===
===Parameters===
{{par begin}}
{{par begin}}
{{par | command | character string identifying the command to be run in the command processor. If 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 pointer is given, command processor is checked for existence}}
{{par end}}
{{par end}}


===Return value===
===Return value===
Implementation-defined value. If {{tt|command}} is {{lc|NULL}} returns nonzero value only if command processor exists.
Implementation-defined value. If {{tt|command}} is returns nonzero value only if command processor exists.


===Notes===
===Notes===
Line 21: Line 21:


===Example===
===Example===
In this example there is a system call of the unix command '''ls -l >test.txt''':
{{example
{{example
  |
  |
  | code=
  | code=
#include <cstdlib>
#include <cstdlib>


int main()
int main()
{
{
     std::system("ls -l >test.txt");
     std::system("ls -l >test.txt"
);
}
}
  | output=
  | output=
}}
}}



Revision as of 20:56, 30 September 2015

 
 
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 (/bin/sh, cmd.exe, command.com) 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 host environment has a command processor and returns nonzero value 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 nonzero value only if command processor exists.

Notes

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

Example

#include <cstdlib>
#include <fstream>
#include <iostream>

int main()
{
    std::system("ls -l >test.txt"); // execute 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