In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains how to solve the interaction problem between C language and command line. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "how to solve the interaction problem between C language and command line"!
In Windows operating systems, files with the suffix.exe are executable files. Exe is the abbreviation of executable, which means executable. All executable files are binary files, and computers can only recognize binary files.
Files with the suffix.exe are compiled from C source files.c or C++ source files.cpp.
In Windows operating systems, any.exe file can be executed as a command on the command line. For example, there are many compiled.exe executable files in the cPro folder of my computer D disk:
Win + R key input cmd, enter the command line:
Then enter the command to D:cPro
In the current cPro file directory, you can enter commands to execute these executable files:
Print a heart chart
Binary characters of arbitrary length converted to hexadecimal
All of the above executable programs are parameterless, directly enter its name, you can execute. The title of this article is Interacting with the command line, that is, receiving arguments from the command line. In most cases, in the C language code we write, the main function has no parameters. If you want to interact with the command line, you need to set the parameters of the main function:
int main(int argc, char *argv[]){ return 0;}
where argc is the number of arguments and argv is an array of strings.
For example: receive parameters from the command line and print out their parameters:
#includeint main(int argc, char *argv[]){ int i; for(i = 0; i < argc; i++) printf("%s",argv[i]); return 0;}
Compile this code, put it in the cPro file directory of drive D, and enter the command line:
where tst is the name of the executable program followed by a bunch of its parameters. argv[0] has a value of tst, argv[1] has a value of Jackey, argv[2] has a value of Song, and so on. The number of parameters is not fixed.
You can also write a program that automatically enters commands on the command line, such as dir to list all files in the current file directory:
int main(){ system("dir"); return 0;}
Run Results:
For example, write a program that calls another program in the current file directory:
int main(){ system("tst"); return 0;}
Run Results:
The above few small cases are a good introduction to how to write C language programs, to interact with the command line, to achieve a C program to call another C program, or to receive parameters from the command line, and to print out the parameters.
Of course, we can implement slightly more complex functions, such as writing a program that automatically creates source files and adding it to environment variables, so that we can use it in any file directory. The following code is the program that automatically creates Java source files:
#include #include int main(int argc, char *argv[]){ char str[20]; char ext[5] = ".java"; int i; FILE *fp[argc]; for(i = 1; i < argc; i++){ strcpy(str,argv[i]); strcat(str,ext); fp[i] = fopen(str,"wb+"); printf("%s Source file created successfully! ",str); fclose(fp[i]); } return 0;}
After compilation, name the generated executable newja, add it to the environment variable, and call it on the command line:
You can also write a program to automatically enter commands on the command line and call newja, which automatically creates source files:
int main(){ system("newja aaa b cc e fdd"); return 0;}
Interacting with the command line can do much more than that. It can do anything you can think of.
At this point, I believe that we have a deeper understanding of "how to solve the interaction problem between C language and command line," so we may as well actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.