zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    BZOJ1001 狼抓兔子 终于过了!
    BZOJ 1901: Zju2112 Dynamic Rankings( 树状数组套主席树 )
    BZOJ 2302: [HAOI2011]Problem c( dp )
    BZOJ 1407: [Noi2002]Savage( 数论 )
    BZOJ 2661: [BeiJing wc2012]连连看( 费用流 )
    BZOJ 1021: [SHOI2008]Debt 循环的债务( dp )
    BZOJ 3170: [Tjoi 2013]松鼠聚会( sort )
    BZOJ 2301: [HAOI2011]Problem b( 数论 )
    BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )
    BZOJ 3231: [Sdoi2008]递归数列( 矩阵快速幂 )
  • 原文地址:https://www.cnblogs.com/7code/p/4915042.html
Copyright © 2011-2022 走看看