Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the system function in C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use the system function in C language", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the system function in C language" article.

Note: commands can be case-insensitive in the system function under windows!

Function name: system

Power: issue a DOS command

Usage: int system (char * command)

The system function has been included in the standard c library and can be called directly.

For example:

# include#includeint main () {printf ("About to spawn and run a DOS command"); system ("dir"); return 0;}

Another example is:

System ("PAUSE") can freeze the screen so that it is easy to observe the execution result of the program.

System ("CLS") can clear the screen.

Calling the color function can change the foreground color and background of the console. The specific parameters are described below.

For example:

Use system ("color 0A"); where 0 after color is the background color code, and An is the foreground 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 = lavender E = light yellow F = bright white

You're welcome to take it. My name is Lei Feng!

In the previous first blog, I also wrote some DOS operation commands. If you want to know, please learn about it. Here are some more for you.

Give an example

Looking at the following example, I believe you will learn more about the application of system in C programming.

Example 1:

Let's try to use C language to call DOS command to realize timing shutdown. It's interesting to think about it. Heh heh: you can get one for your friend's computer and let him input that I am a pig so that he can not turn it off or else it will turn off. If you want to get other fields, you can modify the words you give in the program. But don't go too far. You can't solve it by yourself.

# include / / Standard I / O library function # include / / you can enter system to type the command # include / / string in cmd under the DOS management window interface, and the array-defined library function int main () {char a [10] / / define an array of character types to facilitate players to type all kinds of character information from the keyboard flag: printf ("Please enter" I am a pig ", or the computer will shut down in two minutes"); / / the expression "I am a pig" can be freely changed by the maker, but remember to change the system ("shutdown-s-t 120") later. / / 60 seconds countdown shutdown command, the time is free to change scanf ("% s", a); / / players type the string if (strcmp (a, "I am a pig") = = 0) {/ / compare two strings from the keyboard, and execute the sentence printf under IF ("Congratulations on your successful selection of your horoscope and take the initiative to admit reality!") System ("shutdown-a"); / / end the automatic shutdown countdown command, of course, you can also break the rule and cancel the statement, but that would be too evil} else goto flag; / / jump statement, jump to the flag mark to continue to execute return 0;}

What this program can achieve is to turn off the computer at a fixed time. You can try it.

# include / / Standard I / O library function # include / / you can enter system to type the command # include / / string in cmd under the DOS management window interface, the array defined library function int print () {printf ("* shutdown program *"); printf ("* 1. Realize the time to shut down the computer within ten minutes); printf ("* 2. Shut down the computer immediately * "); printf (" * 3. Log out of the computer * "); printf (" * 4. Exit system * "); 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 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) "); 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! ");} system (" pause ") Exit (0);}

Example 2:

Delete a file in C language, for example, the location of the file is d:123.txt

Execute the windows command with the system () function.

# include#includeint main () {system ("del d:123.txt"); return 0;} the above is about "how to use the system function in C language". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report