Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use system to call the system command line in C language

Shulou Source: shulou.com Published: 2022-06-02 04:25:16 09月17日 Update

This article introduces the knowledge of "how C language uses system to call the command line of the system". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

System, as its name implies, invokes the system command line, enters it as a string, and then outputs the string to the command line for execution.

To test its features, make a Mini Program:

/ / system.c#include#include#includeint main () {char cmd [100]; while (1) {printf ("input code:"); gets (cmd); if (strcmp (cmd, "exit") = = 0) break; / / exit system (cmd) when entering exit;} return 0;}

And then start.

> gcc system.c > a.exeinput code: asdfasdf'asdfasdf' is not an internal or external command, nor is it a runnable program or batch file. Input code: date current date: 2021-12-19 Sunday enter a new date: (year, month, day) input code: date current date: 2021-12-19 Sunday enter a new date: (year, month, day) input code: HELP for more information about a command, type the HELP command name ASSOC to display or modify the file extension association. ATTRIB displays or changes file properties. BREAK sets or clears extended CTRL+C checks. BCDEDIT sets properties in the startup database to control startup loading. # because it's too long and the result is the same as typing HELP on the command line, the details of the tool are omitted here, see the command line reference in the online help. Input code: exit # exit

With system, you can make an enhanced command line.

In addition to the commands provided by these terminals, you may also need some custom statements, which are stored in the environment variables, and getenv can get the environment variables corresponding to the names.

Char * getenv (const char * name)

For example:

# include # include int main () {printf ("PATH:% s\ n", getenv ("PATH")); return 0;}

The running results are as follows:

E:\ Documents\ 00\ 1220 > a.exe

PATH: C:\ Program Files\ Microsoft\ jdk-11.0.12.7-hotspot\ bin;C:\ Python310\ Scripts\; C:\ Python310\; C:\ Program Files\ Common Files\ Oracle\ Java\ javapath;D:\ CS\ ImageMagick; (x86)\ Common Files\ Intel\ Shared

....

This is the end of the content of "how C language uses system to call the system command line". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Commands inputs dates systems files related languages information content variables characters strings attributes years months days more environment knowledge programs results Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Redmi MySQL Microsoft Apple