目录

int system(const char *string)

描述 (Description)

C库函数int system(const char *command)将命令指定的命令名或程序名传递给要由命令处理器执行的主机环境,并在命令完成后返回。

声明 (Declaration)

以下是system()函数的声明。

int system(const char *command)

参数 (Parameters)

  • command - 这是包含所请求变量名称的C字符串。

返回值 (Return Value)

错误时返回的值为-1,否则返回命令的返回状态。

例子 (Example)

以下示例显示了使用system()函数列出unix机器下当前目录中的所有文件和目录。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
   char command[50];
   strcpy( command, "ls -l" );
   system(command);
   return(0);
} 

让我们编译并运行上面的程序,它将在我的unix机器上产生以下结果 -

drwxr-xr-x 2 apache apache 4096 Aug 22 07:25 hsperfdata_apache
drwxr-xr-x 2 railo railo 4096 Aug 21 18:48 hsperfdata_railo
rw------ 1 apache apache 8 Aug 21 18:48 mod_mono_dashboard_XXGLOBAL_1
rw------ 1 apache apache 8 Aug 21 18:48 mod_mono_dashboard_asp_2
srwx---- 1 apache apache 0 Aug 22 05:28 mod_mono_server_asp
rw------ 1 apache apache 0 Aug 22 05:28 mod_mono_server_asp_1280495620
srwx---- 1 apache apache 0 Aug 21 18:48 mod_mono_server_global

以下示例显示了使用system()函数列出Windows机器下当前目录中的所有文件和目录。

#include <stdio.h>
#include <string.h>
int main () {
   char command[50];
   strcpy( command, "dir" );
   system(command);
   return(0);
} 

让我们编译并运行上面的程序,它将在我的Windows机器上产生以下结果 -

a.txt
amit.doc
sachin
saurav
file.c
↑回到顶部↑
WIKI教程 @2018