About calling the system function __ function in C + +

Source: Internet
Author: User

Let's take a look at the following procedure:

#include <iostream>

#include <cstdlib>

int main (int argc, char* argv[])

{

Std::cout << "Hello world!" << Std::endl;

Std::system ("pause"); To suspend a program

return 0;

}

And then, a few more pieces of background knowledge.

Background knowledge 1: Character interface

Say more than 10 20 years ago, a youthful glow, handsome, high-spirited, coder sitting in the school room 286 computer, the computer screen is a black box, then use the operating system called DOS, later, a classmate rich, bought a 386, the operating system above is windows 3.1. People say, that is the legendary "GUI" operating system, "graphical user interface".

As such, the operating system has a "character user interface" and "Graphical user interface" points.

Background Knowledge 2: Console

In the graphical user interface of the operating system, often with a simulated "character interface" small system, used to let low-level users directly input control commands to achieve and system interaction, which is called "Console." Therefore, strictly speaking, the "console" is not a DOS operating system, because it is only in the graphical user interface of the operating system analog, embedded a "character interface" small system, and can not be isolated.

By clicking on the "Start" button, find the "Run (R)" menu item in the Start menu, click, enter cmd, press ENTER, or click the "OK" button to see a black window. So, it opens up the console that sounds pretty iffy.

Background 3: Low-level and advanced

Perhaps you will ask: "Low-level users directly in the console input control commands to achieve and system interaction, then, advanced users."

Well. This question is well asked. The "low-level" here is simply to say that some users have a deeper understanding of the operating system, so that they can use some of the relatively "low-level" features, in fact, all users are equal. "Advanced" users are the keyboard and other devices, especially the mouse to interact with the graphical interface of the operating system. For example, everyone will drag through the mouse to move a file from one file to another folder, right. There's No. First go to the basic operation of Windows to learn how to program it.

In the terminology of computers, "low-level" often means close to the underlying logic, such as hardware. Thus, "low-level" often denotes a "difficulty". For example, we learn C + + is called "Advanced language" (Java or C # more advanced), C can be called "intermediate language", and assembly language is called "low-level language", but you know, assembly language is very difficult oh.

Here we go: the system function

To invoke this function, you must include in the preceding: #include <cstdlib>

Repeat: c means that this function is also common in C language, STD standard (of course, C standard OH), lib means "library." is the standard library in C language. Then system is a standard function in the C language standard library.

The system function can execute the console command you issued. Of course, you have to enclose the command in English double quotes to indicate a sentence. However, you can also enter this command directly in the console, of course, then do not use quotes. We've learned how to get a console window out of the "run" command, and now you just type pause in that window to try.

Finally, I would say that the function or code associated with the console is generally not available in the graphical user interface.

Example: A program to achieve a few simple DOS functions, including: Mtype, Mcopy, Mcomp and so on.

Mtype implementation DOS type function, display. Contents of TXT file

Mcopy implements the DOS copy function, copying a free-form disk file.

Mcomp realizes the DOS comp function, compares two arbitrary format disk files, from the length, the content two aspects.

Note the main function with parameters.

Idea: The command string is constructed from the parameters of the main function, and then executed using the system function call.

int main (int argc,char **argv) {...}

――――――――――――――――――――――――――――

Homework:

1. Change the word "pause" in the system ("pause") to "pause" and try to determine whether the console command is case-sensitive.

2. Before system ("pause"), insert a row: Std::system ("dir"); Compile, execute, and see what the result is.

――――――――――――――――――――――――――――

Reader's question: I enter the following code in the main program section

System ("D:\dir");

System ("dir d:");

System ("ping xxx.xxx.xxx.xxx");

System ("Copy D:\1.txt e:\2.txt");

The output is not responding, the program does not run the error. The included files are included, the written format is not wrong, it should reflect the bar. Thank you for your reply!!

Question added: So how do you see the effect? For example, you want to see a list of files under D disk, like running commands in MS-DOS. In addition, the last copy is not implemented, the disk is really a file.

Reply: The compiler will not check your input parameter string, so even if your argument (string) is wrong, it will certainly not report an error. All of your calls are independent, and the operation is actually performed, but you can't see them. The only way to see the effect should be the last one, if the file exists, you should be able to copy the successful. If you want to be able to see the effect of several other instructions, you might as well add a sentence at the end: System ("pause") replied: "I don't quite understand your meaning, so I say it according to my understanding."

You should understand that the system () function is for DOS interface operations, that is, call commands in the DOS command library to complete the operation, so generally only some information can be processed and displayed on the DOS interface, but rarely used to extract information.

If you want to view the contents of a folder in DOS, use the dir command, and the system () function to write:

......

System ("dir [pathname]");

......

Where the path name is optional, that is, if it is the current directory, it can be ignored. Note, however, that when writing a path, ' \ ' is to be represented by ' \ ', because this is the escape character of the C language. If a C:\abc is to be specified, it should be written as follows:

System ("dir c:\\abc");

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.