About calling the system 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"); Let the program pause

return 0;

}

Then, a few more background knowledge.

Background 1: Character interface

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

Thus, the operating system has a "character user interface" and a "graphical user interface" of the points.

Background 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 simulated, embedded in a "character interface" small system, and can not exist alone.

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

Background 3: Low and advanced

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

Well! The question is well asked. The "low-level" here simply means that some users have a deeper understanding of the operating system, and thus can use some relatively "bottom-up" functions, in fact all users are equal. "Advanced" users are using devices such as keyboards, especially mice, to interact with the operating system of the graphical interface. For example, everyone will be dragged through the mouse to implement moving a file from one file to another file folder, right? Is that a no? First go to the basic operation of Windows learn to learn programming again.

In computer terminology, "low-level" often means approaching the underlying logic, such as hardware. Therefore, "low-level" often indicates a "difficulty". For example, we learn C + + called "high-level language" (Java or C # more advanced), C can be called "intermediate language", and assembly language is called "low-level language", but you have to know, assembly language is very difficult oh.

Here's the beginning: System functions

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

Repeat: c means that this function is also common in C, STD is standard (of course c), lib means "library". 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 double quotation marks in English to express a sentence. However, you can also enter this command directly from the console, which, of course, does not have to be quoted at that time. We've learned how to run a console window by running the command, and now you just have to enter pause in that window to try it out.

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

Example: Compiling a program to achieve a few simple DOS functions, including: Mtype, Mcopy, Mcomp, etc.

The Mtype implements the DOS type feature, which is displayed. Contents of TXT file

Mcopy implements the DOS copy function and copies a disk file in any format.

Mcomp implements the DOS comp function, comparing two disk files in any format, from two aspects of length and content.

Note the main function with parameters.

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

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

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

Homework:

1. Change the word "pause" in System ("pause") to "pause", and try the console command is case-sensitive?

2. Insert a line before system ("Pause"): Std::system ("dir"); Compile, execute, and see what the results are?

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

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, and the program is running without error. The included files are all included, the format of the writing is not wrong, should be reflected in it? Thanks for the reply!!

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

Reply: The compiler will not check your input parameter string, so even if your argument (string) is wrong, it will certainly not error. Your calls are all independent, and the operation is actually performed, but you can't see them. The only thing that can see the effect should be the last one, if the file exists, it should be able to copy the success. If you want to see the effects of several other instructions in a perceptual way, add one sentence at the end: System ("pause")

Answer: I don't quite understand what you mean, so I say it according to my understanding.

You have to understand that the system () function is for DOS interface operations, that is, to invoke the command in the DOS command library to complete the relevant operation, so it is generally only to process some information and display on the DOS interface, and rarely used to extract information.

If you want to view the contents of a folder under DOS, you can use the dir command, specifically to the system () function:

......

System ("dir [path name]");

......

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 represented by ' \ \ ' because it is the escape character of the C language. If you want to specify C:\ABC, you should write this:

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

About calling the system function in C + +

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.