C Language System function

Source: Internet
Author: User
Tags clear screen

Let's take a look at the system () function (mainly in C language application) under Windows operating system.
Note: commands in the system function under Windows can be case-insensitive.
Function Name: System
Function: Issue a DOS command
Usage: int system (char *command);

The system function has been included in the standard C library and can be invoked directly.

For example:

#include <stdio.h>
#include <stdlib.h>
int main ()
{
    printf ("About to spawn and run a DOS") Command\n ");
    System ("dir");
return 0;
}

Another example:
System ("PAUSE") can freeze the screen to observe the execution result of the program;
System ("CLS") enables clear screen operation.
The call to the color function can change the foreground color and background of the console, as described in the following parameters.

For example:
With System ("Color 0A"); One of the color behind the 0 is the background color code, a is the front color code. The color codes are as follows:

0= Black 1= Blue 2 = Green 3= Lake Blue 4 = Red 5 = Purple 6 = Yellow 7 = White 8 = Gray 9 = light blue a= light green b= light green c= light red d= light purple e= light yellow f= bright white
Thank you, my name is Lei Feng.

In the first blog I also wrote a few DOS operation commands you want to understand the next to understand. Here are some more for you;

examples
Look at the following examples, I believe you will learn more system in C programming application.
Example One:
Let's try a C language to call the DOS command to implement a timed shutdown, think about it is also very interesting, hey: we can give you a friend on the computer to get a oh let him input I am a pig can not turn off the machine or shut down. If you want to get other fields, you can change the word you gave to the program. But let's not overdo it. Be careful of yourself can not solve oh.

#include <stdio.h>//     standard input/output library function

#include <stdlib.h>    //You can enter the command in CMD under the DOS Management window interface by typing system

#include <string.h>    //String, array-defined library function

int main ()

{

    char a[10];    Defines an array of character types to make it easy for players to type all kinds of character information from the keyboard

flag:

    printf ("Please enter \" I am pig \ ", otherwise the computer will be closed in two minutes \ n");  "I'm a pig." This statement can be freely changed by the creator, but remember to change the

    system ("shutdown-s-T") at the appropriate location;    60 seconds Countdown Shutdown command, time is free to change

    scanf ("%s", a);             Player type string from keyboard

    if (strcmp (A, "I am pig") = = 0) {  //Compare two strings, if equal then execute the statement

        printf ("Congratulations on your successful choice of the Zodiac, the initiative to admit the reality!\n");

        System ("Shutdown-a");  End the automatic shutdown Countdown command, of course, can also break the rules, cancel the statement, but that would be too evil

    }

    else

        goto flag;        Jump statement, jump to the flag tag to continue with return

    0;

This program can be achieved is the timing shutdown we can go to try Oh

#include <stdio.h>//Standard input/Output library function #include <stdlib.h>//You can enter the command in CMD under the DOS admin window interface #include <strin
    g.h>//String, array-defined library function int print () {printf ("**************** Shutdown Program ****************\n");
    printf ("****1. Implementation in 10 minutes of timed shutdown of the computer ****\n");
    printf ("************2. Turn off computer ************\n immediately");
    printf ("***************3. Log off Computer *************\n");
    printf ("****************4. Exit system **************\n");

return 0;  } void Main () {System ("Title C language Shutdown Program");/Set CMD window width system ("mode con cols=48 lines=25");/window Width height system ("color
    0B ");/Set the default console foreground background color 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 ("How many seconds do you want to turn off the computer automatically?")
        (0~600) \ n ");
        scanf ("%s", T);
        System (strcat (cmd, t));
    Break
        Case 2:system ("shutdown-p");
    Break
       Case 3:system ("shutdown-1"); Break
    Case 0:break;
    default:printf ("error!\n");
    System ("pause");

Exit (0); }

Example Two:
Delete a file in C language, such as the location of the file is D:\123.txt
Use the system () function to execute Windows commands.

#include <stdio.h>
#include <stdlib.h>
int main ()
{
    system ("Del d:\123.txt");
    return 0;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.