std::system
来自cppreference.com
工具库
程序支持工具
程序终止 | |||||||||||||||||||||
|
|||||||||||||||||||||
不可达控制流 | |||||||||||||||||||||
(C++23)
|
|||||||||||||||||||||
与环境交流 | |||||||||||||||||||||
|
|||||||||||||||||||||
信号 | |||||||||||||||||||||
信号类型 | |||||||||||||||||||||
非局部跳转 | |||||||||||||||||||||
类型 | |||||||||||||||||||||
在标头
<cstdlib>
定义
|
||
int system( const char* command );
|
||
以参数 command
调用运行环境的命令处理器(例如 /bin/sh
、 cmd.exe
、
command.com
)。返回相应实现的定义值(通常是被调用程序所返回的值)。
若 command
为空,则检查运行环境是否有命令处理器,并当且仅当命令行存在才返回非零。
参数
command | - | 标识要运行于命令处理器的命令字符串。若给出空指针,则检查命令处理器的存在性 |
返回值
实现定义值。若 command
为空指针,则当且仅当命令处理器存在才返回非零值。
注意
POSIX 系统上,可用 WEXITSTATUS 和 WSTOPSIG 分解返回值。
相关的 POSIX 函数 popen
使调用方可获取到 command
生成的输出。
示例
运行此代码
#include <cstdlib> #include <fstream> #include <iostream> int main() { std::system("ls -l >test.txt"); // 执行 UNIX 命令 "ls -l >test.txt" std::cout << std::ifstream("test.txt").rdbuf(); }
可能的输出:
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