system 函数

1.函数详解

system函数需加头文件<stdlib.h>后方可调用

功能:发出一个DOS命令
用法:int system(char *command);

程序例:

1 #include <stdlib.h>
2 #include <stdio.h>
3 
4 int main()
5 {
6     printf("Run a DOS command
");
7     system("pause"); //暂停屏幕刷新
8     return 0;
9 }

2.函数参数

程序例:

 1 system("pause"); //暂停屏幕刷新
 2 
 3 system("CLS"); //可以实现清屏操作。
 4 
 5 system("calc.exe"); //运行计算器
 6 
 7 system("notepad"); //打开记事本程序
 8 
 9 system("ipconfig >> 123.txt"); //输出ipconfig查询出的结果到当前目录的123.txt文件中,每次都是覆盖的。
10 
11 
12 
13 system("color 0A");  //设置颜色,其中color后面的0是背景色代号,A是前景色代号。
14 
15 // 各颜色代码如下:
16 // 0=黑色 1=蓝色 2=绿色 3=湖蓝色 4=红色 5=紫色 6=黄色 7=白色 8=灰色 9=淡蓝色 A=淡绿色 B=淡浅绿色 C=淡红色 D=淡紫色 E=淡黄色 F=亮白色

3.执行shell 命令

用法:system(执行的shell 命令)

程序例:

1 #include<stdlib.h>
2 
3 void main()
4 {
5     system(“ls -al /etc/passwd /etc/shadow”);
6 }

4.应用

程序例一:

 1 //调用DOS命令实现定时关机
 2 
 3 #include<stdio.h>
 4 #include<string.h>
 5 #include<stdlib.h>
 6 
 7 int print()
 8 {
 9     printf( " ╪╪╪╪╪╪╧╧╧╧╧╧╧╧╪╪╪╪╪╪
" );
10     printf( "╔═══╧╧ 关机程序 ╧╧═══╗
" );
11     printf( "║※1.实现10分钟内的定时关闭计算机 ║
" );
12     printf( "║※2.立即关闭计算机  ║
" );
13     printf( "║※3.注销计算机  ║
" );
14     printf( "║※0.退出系统  ║
" );
15     printf( "╚═══════════════════╝
" );
16 
17     return 0;
18 }
19 
20 void main()
21 {
22     system( "title C语言关机程序" );//设置cmd窗口标题
23     system( "mode con cols=48 lines=25" );//窗口宽度高度
24     system( "color 0B" );
25     system( "date /T" );
26     system( "TIME /T" );
27 
28     char cmd[20] = "shutdown -s -t ";
29     char t[5] = "0";
30     print();
31 
32     int c;
33     scanf( "%d", &c );
34     getchar();
35 
36     switch ( c )
37     {
38     case 0:
39         break;
40     case 1:
41         printf( "您想在多少秒后自动关闭计算机(0~600)
" );
42         scanf( "%s", t );
43         system( strcat( cmd, t ) ); break;
44     case 2:
45         system( "shutdown -p" ); break;
46     case 3:
47         system( "shutdown -l" ); break;
48     default:
49         printf( "Error!
" );
50     }
51 
52     system( "pause" );
53     exit( 0 );
54 }

程序例二:

 1 //执行windows命令删除文件,例如文件的位置是d:123.txt
 2 
 3 #include <stdlib.h>
 4 #include <stdio.h>
 5 
 6 int main()
 7 {
 8     system( "del d:\123.txt" );
 9     return 0;
10 }
原文地址:https://www.cnblogs.com/7code/p/4915042.html