C 库函数 - system()

描述

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

声明

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

  1. int system(const char *command)

参数

  • command — 包含被请求变量名称的 C 字符串。

返回值

如果发生错误,则返回值为 -1,否则返回命令的状态。

实例

下面的实例演示了 system() 函数的用法,列出了 unix 机上当前目录下所有的文件和目录。

实例

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include<stdlib.h>
  4. int main ()
  5. {
  6. char command[50];
  7. strcpy( command, "ls -l" );
  8. system(command);
  9. return(0);
  10. }

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

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

下面的实例演示了 system() 函数的用法,列出了 windows 机上当前目录下所有的文件和目录。

  1. #include <stdio.h>
  2. #include <string.h>
  3. int main ()
  4. {
  5. char command[50];
  6. strcpy( command, "dir" );
  7. system(command);
  8. return(0);
  9. }

让我们编译并运行上面的程序,在 windows 机上将产生以下结果:

  1. a.txt
  2. amit.doc
  3. sachin
  4. saurav
  5. file.c