命名空间
变体
操作

std::system

来自 cppreference.cn
< cpp‎ | utility‎ | program
 
 
 
 
定义于头文件 <cstdlib>
int system( const char* command );

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

如果 command 是空指针,则检查宿主环境是否具有命令处理器,并且仅当命令处理器存在时才返回非零值。

目录

[编辑] 参数

command - 标识要在命令处理器中运行的命令的字符串。如果给定空指针,则检查命令处理器是否存在

[编辑] 返回值

实现定义的值。如果 command 是空指针,则仅当命令处理器存在时才返回非零值。

[编辑] 注释

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

相关的 POSIX 函数 popen 使 command 生成的输出可供调用者使用。

如果派生的进程执行任何屏幕 I/O,则在调用 std::system 之前,也必须显式刷新 std::cout

[编辑] 示例

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

可能的输出

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

[编辑] 参见

C 文档 关于 system