system、_wsystemsystem, _wsystem

执行命令。Executes a command.


此 API 不能用于在 Windows 运行时中执行的应用程序。This API cannot be used in applications that execute in the Windows Runtime. 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数For more information, see CRT functions not supported in Universal Windows Platform apps.


int system(
   const char *command
int _wsystem(
   const wchar_t *command


要执行的命令。The command to be executed.

返回值Return Value

如果命令NULL并找到命令解释器,则返回一个非零值。If command is NULL and the command interpreter is found, returns a nonzero value. 如果未找到命令解释器,返回 0 并设置errnoENOENTIf the command interpreter is not found, returns 0 and sets errno to ENOENT. 如果命令NULL系统返回由命令解释器返回的值。If command is not NULL, system returns the value that is returned by the command interpreter. 仅当命令解释器返回值 0 时,它才会返回值 0。It returns the value 0 only if the command interpreter returns the value 0. 返回值-1 指示错误,和errno设置为以下值之一:A return value of - 1 indicates an error, and errno is set to one of the following values:

E2BIGE2BIG 自变量列表(与系统相关)太大。The argument list (which is system-dependent) is too big.
ENOENTENOENT 无法找到命令解释器。The command interpreter cannot be found.
ENOEXECENOEXEC 由于格式无效,无法执行命令解释器文件。The command-interpreter file cannot be executed because the format is not valid.
ENOMEMENOMEM 没有足够的内存可用于执行命令;或可用内存已损坏;或存在无效块,这表明进行调用的进程未正确进行分配。Not enough memory is available to execute command; or available memory has been corrupted; or a non-valid block exists, which indicates that the process that's making the call was not allocated correctly.

有关这些返回代码的详细信息,请参阅 _doserrno、errno、_sys_errlis 和 _sys_nerrSee _doserrno, errno, _sys_errlist, and _sys_nerr for more information about these return codes.


系统函数传递命令到命令解释器,其中执行与操作系统命令字符串。The system function passes command to the command interpreter, which executes the string as an operating-system command. 系统使用COMSPEC路径环境变量来定位命令解释器文件 CMD.exe。system uses the COMSPEC and PATH environment variables to locate the command-interpreter file CMD.exe. 如果命令NULL,该函数只检查命令解释器是否存在。If command is NULL, the function just checks whether the command interpreter exists.

您必须显式刷新,通过使用fflush_flushall,或关闭任何流,然后才能调用系统You must explicitly flush, by using fflush or _flushall, or close any stream before you call system.

_wsystem是宽字符版本的系统;命令参数 _wsystem是宽字符字符串。_wsystem is a wide-character version of system; the command argument to _wsystem is a wide-character string. 否则这些函数具有相同行为。These functions behave identically otherwise.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tsystem_tsystem systemsystem systemsystem _wsystem_wsystem


例程Routine 必需的标头Required header
systemsystem <process.h> 或 <stdlib.h><process.h> or <stdlib.h>
_wsystem_wsystem <process.h> 或 <stdlib.h> 或 <wchar.h><process.h> or <stdlib.h> or <wchar.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


此示例使用系统以键入一个文本文件。This example uses system to TYPE a text file.

// crt_system.c

#include <process.h>

int main( void )
   system( "type crt_system.txt" );

输入:crt_system.txtInput: crt_system.txt

Line one.
Line two.


Line one.
Line two.

请参阅See also

进程和环境控制Process and Environment Control
_exec、_wexec 函数_exec, _wexec Functions
exit、_Exit、_exitexit, _Exit, _exit
_spawn、_wspawn 函数_spawn, _wspawn Functions