1、system函数:
原型:int system(const char * command);
功能:执行 dos(windows系统) 或 shell(Linux/Unix系统) 命令,参数字符串command为命令名;
说明:在windows系统中,system函数直接在控制台调用一个command命令。在Linux/Unix系统中,system函数会调用fork函数产生子进程,由子进程来执行command命令,命令执行完后随即返回原调用的进程;
头文件:stdlib.h;
返回值:命令执行成功返回0,执行失败返回-1。
2、例程:
#include
#include
int main(){
system("del C:\\123.txt");//在控制台中,执行命令del C:\\123.txt,删除C盘目录下的123.txt文件
return 0;
}
#include
#include
int main(void){
char arr[100]="ping ";
char buf[20]={};
scanf("%s",buf);
strcat(arr,buf);
printf("arr:%s\n",arr);
system(arr);
}
结果:
~/test$ ./a.out
192.168.0.200
arr:ping 192.168.0.200
PING 192.168.0.200 (192.168.0.200) 56(84) bytes of data.
64 bytes from 192.168.0.200: icmp_req=1 ttl=64 time=0.090 ms
64 bytes from 192.168.0.200: icmp_req=2 ttl=64 time=0.028 ms
64 bytes from 192.168.0.200: icmp_req=3 ttl=64 time=0.040 ms
输入字符串即可。
比如:要ping一个ip地址,格式如下:
system("ping 192.168.1.1");
要暂停,格式如下:
system("pause");