C 庫函數(shù) - system()
描述
C 庫函數(shù) int system(const char *command) 把 command 指定的命令名稱或程序名稱傳給要被命令處理器執(zhí)行的主機環(huán)境,并在命令完成后返回。
聲明
下面是 system() 函數(shù)的聲明。
int system(const char *command)
參數(shù)
- command -- 包含被請求變量名稱的 C 字符串。
返回值
如果發(fā)生錯誤,則返回值為 -1,否則返回命令的狀態(tài)。
實例
下面的實例演示了 system() 函數(shù)的用法,列出了 unix 機上當前目錄下所有的文件和目錄。
#include <stdio.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() 函數(shù)的用法,列出了 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
更多建議: