std::system

来自cppreference.com
< cpp‎ | utility‎ | program
 
 
 
 
在标头 <cstdlib> 定义
int system(constchar* command );

以参数 command 调用宿主环境的命令处理器(例如 /bin/shcmd.exe)。返回由实现定义的值(通常是被调用程序所返回的值)。

command 为空指针,则检查运行环境是否有命令处理器,并当且仅当命令处理器存在时返回非零。

目录

[编辑]参数

command - 标识要在命令处理器中运行的命令的字符串。若给出空指针,则检查命令处理器的存在性

[编辑]返回值

由实现定义的值。若 command 为空指针,则当且仅当命令处理器存在时返回非零值。

[编辑]注解

POSIX 系统上,可用 WEXITSTATUSWSTOPSIG 分解返回值。

相关的 POSIX 函数 popen 使调用方可获取 command 生成的输出。

如果孵化的进程进行任何屏幕 I/O 的话,显式冲洗 std::cout 在调用 std::system 前也是必须的。

[编辑]示例

#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

[编辑]参阅

system 的 C 文档
close