c语言函数system C语言函数定义

C语言的system函数

system(系统)函数

创新互联主营柘荣网站建设的网络公司,主营网站建设方案,重庆APP软件开发,柘荣h5小程序开发搭建,柘荣网站营销推广欢迎柘荣等地区企业咨询

windows操作系统下system () 函数详解(主要是在C语言中的应用)

功 能: 发出一个DOS命令

用 法: int system(char *command);

system函数已经被收录在标准c库中,可以直接调用

程序例:

#include stdlib.h

#include stdio.h

int main(void)

{

printf("About to spawn and run a DOS command\n");

system("dir");

return 0;

}

又如:system("pause")可以实现冻结屏幕,便于观察程序的执行结果;system("CLS")可以实现清屏操作。而调用color函数可以改变控制台的前景色和背景,具体参数在下面说明。

例如,用 system("color 0A"); 其中color后面的0是背景色代号,A是前景色代号。各颜色代码如下:

0=黑色 1=蓝色 2=绿色 3=湖蓝色 4=红色 5=紫色 6=黄色 7=白色 8=灰色 9=淡蓝色 A=淡绿色 B=淡浅绿色 C=淡红色 D=淡紫色 E=淡黄色 F=亮白色

(注意:Microsoft Visual C++6.0 支持system)

举例

看了下面实例,相信你会对学到更多system在C程序设计中的应用。

例一:

C语言调用DOS命令实现定时关机:

#includestdio.h

#includestring.h

#includestdlib.h

int print()

{

printf(" ╪╪╪╪╪╪╧╧╧╧╧╧╧╧╪╪╪╪╪╪\n");

printf("╔═══╧╧C语言关机程序 ╧╧═══╗\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 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;

case 0:break;

default:printf("Error!\n");

}

system("pause");

exit(0);

}

例二:

用C语言删除文件,例如文件的位置是d:\123.txt

用system()函数执行windows命令。

#include stdlib.h

#include stdio.h

int main(void)

{

system("del d:\\123.txt");

return 0;

}

c语言system函数用法

c语言是常用的编程语言,也是很多人接触和学过的,今天就来介绍下c语言中system函数的用法。

软件:Dev-C++ 5.11

电脑:华为MateBook14

系统:Windows10

1、c语言中system函数可以调用DOS命令,在使用这个函数之后,首先要给程序添加#include process.h的头文件,如下图所示。

2、然后,可以先将main函数补充完整,接下来只需要在这个函数中写代码就可以了,如下图所示。

3、System函数可以直接使用,其参数就是需要执行的DOS命令,如下图所示,是用system函数来执行dos中的dir命令。

4、先调试程序,成功之后,点击“运行”程序,如下图所示。

5、这样,就在c语言中使用system函数执行了DOS命令,如下图所示。

在C语言中,程序有一个是system("CLS");时什么意思?

在C语言程序中是清屏的意思。

当你编写的程序有输出的时候,如果要进行多次调试,屏幕上会显示很多次的输出的结果,看上去非常的复杂非常的乱。那么我们就可以在程序中的输出语句之前加上“system("CLS");”,当我们用上这条语句之后。

这样每次程序运行的时候都会将上一次运行输出的内容给清除掉,屏幕上只显示本次输出的结果。这样看起来就非常的简洁。

扩展资料:

在VC环境下有两种办法实现清屏:

1、#include windows.h

system("cls");这种办法的缺点是程序额外运行系统程序执行清屏操作,延长了程序执行时间。

2、自己写函数,这种办法快

这是从微软MSDN得到的方法:

/* Standard error macro for reporting API errors */

#define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error %d from %s \

on line %d\n", __FILE__, GetLastError(), api, __LINE__);}

void cls( HANDLE hConsole )

{

COORD coordScreen = { 0, 0 }; /* here's where we'll home the

cursor */

BOOL bSuccess;

DWORD cCharsWritten;

CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */

DWORD dwConSize; /* number of character cells in

the current buffer */

/* get the number of character cells in the current buffer */

bSuccess = GetConsoleScreenBufferInfo( hConsole, csbi );

PERR( bSuccess, "GetConsoleScreenBufferInfo" );

dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

/* fill the entire screen with blanks */

bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',

dwConSize, coordScreen, cCharsWritten );

PERR( bSuccess, "FillConsoleOutputCharacter" );

/* get the current text attribute */

bSuccess = GetConsoleScreenBufferInfo( hConsole, csbi );

PERR( bSuccess, "ConsoleScreenBufferInfo" );

/* now set the buffer's attributes accordingly */

bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes,

dwConSize, coordScreen, cCharsWritten );

PERR( bSuccess, "FillConsoleOutputAttribute" );

/* put the cursor at (0, 0) */

bSuccess = SetConsoleCursorPosition( hConsole, coordScreen );

PERR( bSuccess, "SetConsoleCursorPosition" );

return;

}

参考资料来源:百度百科-system("cls")


分享名称:c语言函数system C语言函数定义
转载来源:http://ybzwz.com/article/ddiospg.html