system在c语言中
‘壹’ c语言的system函数
system(系统)函数
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命令实现定时关机:
#include<stdio.h>
#include<string.h>
#include<stdlib.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是什么意思
向操作系统传递命令行,具体这个程序段,意思就是将redadd字符串作为命令行传递给操作系统去执行,system(regadd);的作用等同于在运行窗口中运行regadd字符串中保存的命令。
‘叁’ C语言中system("pause")是什么作用和意思
暂停执行,按任意键继续,一般需要把执行过程中的执行信息打印出来时就可以加这么一条,方便查看执行结果。
‘肆’ 在c语言中system有什么功能,如何使用
可以调用系统命令,如system("pause"),可以使程序暂停,保持窗口显示,否则程序就立刻退出了
‘伍’ 在C语言中,程序有一个是system("CLS");时什么意思
在C语言程序中是清屏的意思。
当你编写的程序有输出的时候,如果要进行多次调试,屏幕上会显示很多次的输出的结果,看上去非常的复杂非常的乱。那么我们就可以在程序中的输出语句之前加上“system("CLS");”,当我们用上这条语句之后。
这样每次程序运行的时候都会将上一次运行输出的内容给清除掉,屏幕上只显示本次输出的结果。这样看起来就非常的简洁。

(5)system在c语言中扩展阅读:
在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 ", __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("pause")是什么作用和意思
system就是调用从程序中调用系统命令(和shell命令).
system("pause")就是从程序里调用“pause”命令;
而“pause”这个系统命令的功能很简单,就是在命令行上输出一行类似于“Press any key to exit”的字,等待用户按一个键,然后返回.
‘柒’ 在c语言中system有什么功能,如何使用
可以调用
系统命令
,如system("pause"),可以使
程序暂停
,保持窗口显示,否则程序就立刻退出了
‘捌’ system在C语言里是什么意思
system()函数功能强大,很多人用却对它的原理知之甚少先看linux版system函数的源码:#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
int system(const char * cmdstring)
{
pid_t pid;
int status;
if(cmdstring == NULL){
return (1);
}
if((pid = fork())<0){
status = -1;
}
else if(pid = 0){
execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
-exit(127); //子进程正常执行则不会执行此语句
}
else{
while(waitpid(pid, &status, 0) < 0){
if(errno != EINTER){
status = -1;
break;
}
}
}
return status;
}
分析一下原理估计就能看懂了:
当system接受的命令为NULL时直接返回,否则fork出一个子进程,因为fork在两个进程:父进程和子进程中都返回,这里要检查返回的pid,fork在子进程中返回0,在父进程中返回子进程的pid,父进程使用waitpid等待子进程结束,子进程则是调用execl来启动一个程序代替自己,execl("/bin/sh", "sh", "-c", cmdstring, (char*)0)是调用shell,这个shell的路径是/bin/sh,后面的字符串都是参数,然后子进程就变成了一个shell进程,这个shell的参数是cmdstring,就是system接受的参数。在windows中的shell是command,想必大家很熟悉shell接受命令之后做的事了。
再解释下fork的原理:当一个进程A调用fork时,系统内核创建一个新的进程B,并将A的内存映像复制到B的进程空间中,因为A和B是一样的,那么他们怎么知道自己是父进程还是子进程呢,看fork的返回值就知道,上面也说了fork在子进程中返回0,在父进程中返回子进程的pid。
windows中的情况也类似,就是execl换了个又臭又长的名字,参数名也换的看了让人发晕的,我在MSDN中找到了原型,给大家看看:
HINSTANCE ShellExecute(
HWND hwnd,
LPCTSTR lpVerb,
LPCTSTR lpFile,
LPCTSTR lpParameters,
LPCTSTR lpDirectory,
INT nShowCmd
);
用法见下:
ShellExecute(NULL, "open", "c:\\a.reg", NULL, NULL, SW_SHOWNORMAL);
你也许会奇怪 ShellExecute中有个用来传递父进程环境变量的参数 lpDirectory,linux中的execl却没有,这是因为execl是编译器的函数(在一定程度上隐藏具体系统实现),在linux中它会接着产生一个linux系统的调用 execve, 原型见下:
int execve(const char * file,const char **argv,const char **envp);
看到这里就会明白为什么system()会接受父进程的环境变量,但是用system改变环境变量后,system一返回主函数还是没变。原因从system的实现可以看到,它是通过产生新进程实现的,从我的分析中可以看到父进程和子进程间没有进程通信,子进程自然改变不了父进程的环境变量。
使用了system函数就能执行dos指令。
#include <stdio.h>
#include <stdlib.h>
xiaoyu()
{
char *a;
int n=0;
FILE *f;
f=fopen("file.bat","w+");/*新建一个批处理*/
if(f==NULL)
exit(1);
a="echo"; /*DOS命令*/
for(n=65;n<=90;n++)/*大写A-Z*/
fprintf(f,"%s %c\n",a,n);/*利用ASCII码输出A-Z,写出批处理*/
fclose(f);
system("file.bat");/*运行批处理*/
}
main()
{
char *string;
xiaoyu();
string="echo C语言的system函数\n";/*输出中文*/
system(string);
system("pause");/*程序暂停*/
}
C中可以使用DOS命令,以后编程通过调用DOS命令很多操作就简单多了。
‘玖’ C语言中system("pause")是什么作用和意思
system(“pause”) 是调用DOS系统的暂停命令 pause 来暂停程序执行,按任意一个键后将继续执行。
Sleep() 是 windows 的 函数,让程序进入睡眠状态,要求参数 时间毫秒,到时间后自动苏醒,程序继续执行。
例如: Sleep(2500); 休眠 2.5 秒,满2.5 秒 后 程序继续运行。
