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

如果 commandNULL,并且找到命令解释器,则返回一个非零值。If command is NULL and the command interpreter is found, returns a nonzero value. 如果未找到命令解释器,则返回 0 并将 errno 设置为 ENOENTIf the command interpreter is not found, returns 0 and sets errno to ENOENT. 如果 command 不为 NULL,则 system 返回由命令解释器返回的值。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:

自变量列表(与系统相关)太大。The argument list (which is system-dependent) is too big.

无法找到命令解释器。The command interpreter cannot be found.

由于格式无效,无法执行命令解释器文件。The command-interpreter file cannot be executed because the format is not valid.

没有足够的内存可用于执行命令;或可用内存已损坏;或存在无效块,这表明进行调用的进程未正确进行分配。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.


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

您必须通过使用 fflush_flushall 进行显式清除,或者在调用 system 之前关闭所有的流。You must explicitly flush—by using fflush or _flushall—or close any stream before you call system.

_wsystemsystem 的宽字符版本;command_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 system system _wsystem


例程所返回的值Routine 必需的标头Required header
system <process.h> 或 <stdlib.h><process.h> or <stdlib.h>
_wsystem <process.h> 或 <stdlib.h> 或 <wchar.h><process.h> or <stdlib.h> or <wchar.h>

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


此示例使用 system 来键入一个文本文件。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、_exit exit, _Exit, _exit
_flushall _flushall
_spawn、_wspawn 函数_spawn, _wspawn Functions