您的位置:首页 > 其它

system 函数

2015-10-27 17:47 281 查看
[b]1.函数详解[/b]

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

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

程序例:

#include <stdlib.h>
#include <stdio.h>

int main()
{
printf("Run a DOS command\n");
system("pause"); //暂停屏幕刷新
return 0;
}


[b]2.函数参数[/b]

程序例:

system("pause"); //暂停屏幕刷新

system("CLS"); //可以实现清屏操作。

system("calc.exe"); //运行计算器

system("notepad"); //打开记事本程序

system("ipconfig >> 123.txt"); //输出ipconfig查询出的结果到当前目录的123.txt文件中,每次都是覆盖的。

system("color 0A");  //设置颜色,其中color后面的0是背景色代号,A是前景色代号。

// 各颜色代码如下:
// 0=黑色 1=蓝色 2=绿色 3=湖蓝色 4=红色 5=紫色 6=黄色 7=白色 8=灰色 9=淡蓝色 A=淡绿色 B=淡浅绿色 C=淡红色 D=淡紫色 E=淡黄色 F=亮白色


[b]3.执行shell 命令[/b]

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

程序例:

#include<stdlib.h>

void main()
{
system(“ls -al /etc/passwd /etc/shadow”);
}


[b]4.应用[/b]

程序例一:

//调用DOS命令实现定时关机

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int print()
{
printf( " ╪╪╪╪╪╪╧╧╧╧╧╧╧╧╪╪╪╪╪╪\n" );
printf( "╔═══╧╧ 关机程序 ╧╧═══╗\n" );
printf( "║※1.实现10分钟内的定时关闭计算机 ║\n" );
printf( "║※2.立即关闭计算机  ║\n" );
printf( "║※3.注销计算机  ║\n" );
printf( "║※0.退出系统  ║\n" );
printf( "╚═══════════════════╝\n" );

return 0;
}

void main()
{
system( "title C语言关机程序" );//设置cmd窗口标题
system( "mode con cols=48 lines=25" );//窗口宽度高度
system( "color 0B" );
system( "date /T" );
system( "TIME /T" );

char cmd[20] = "shutdown -s -t ";
char t[5] = "0";
print();

int c;
scanf( "%d", &c );
getchar();

switch ( c )
{
case 0:
break;
case 1:
printf( "您想在多少秒后自动关闭计算机(0~600)\n" );
scanf( "%s", t );
system( strcat( cmd, t ) ); break;
case 2:
system( "shutdown -p" ); break;
case 3:
system( "shutdown -l" ); break;
default:
printf( "Error!\n" );
}

system( "pause" );
exit( 0 );
}


程序例二:

//执行windows命令删除文件,例如文件的位置是d:\123.txt

#include <stdlib.h>
#include <stdio.h>

int main()
{
system( "del d:\\123.txt" );
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: