The principle summary of command line parameter in C + + __c++

Source: Internet
Author: User

In C + +, the transfer of command line parameters is realized by using main parameter transfer

"1" To implement command-line arguments we will define the form using main (int argc,char* argv[]) argc and argv can be replaced with your own favorite name not necessarily ARGV,ARGC these forms are just habits, char* argv[] As we've already talked about, this is a pointer array, argv is a pointer array name, ARGV is not a constant pointer, but a variable pointer with variable characteristics, it can be moved, so we can write char* *argv is correct, int ARGC This definition returns the number of parameters so it is marked as a cosmetic (int).

For example: Write a command file and print the string in reverse order. Set file name to Invert.c

#include <stdio.h> #include <conio.h> int main (int argc,char *argv[]) {int i; for (i=argc-1;i>0;i--) printf ("%s", Argv[i]); Getch (); return 0; }

Get the exe file path after you translate the generated
Run-> input cmd
Switch to disc: Enter the letter with a colon
CD path
For example: compiled programs in D:/work/invert.exe
Run->cmd
D:
Cd/work
Invert I Love

"2" in the actual program we often have to analyze command line parameters. For example, we have a program a can accept many parameters. One possible situation is
a-d Print--option1 Hello--option2 world

So how do we analyze the parameters of this command? Often using functions is getopt and Getopt_long.

#include <unistd.h> #include <getopt.h> int getopt (int argc,char const **ARGV, const char *optstring); int getopt_long (int argc,char const **ARGC,CONST char string,const strUCt option *longopts, int *longindex); extern char *optarg;    extern int optind,opterr,optopt; struct option {char *name; int has_flag; int *flag; int value;};

Getopt_long is an extension of getopt. Getopt accepts command-line arguments that can only begin with (-), and Getopt_long can accept (--) arguments at the beginning of a (-). Usually the label of the parameter with (-) begins with only one letter, and (-) The argument at the beginning can be a string. As the-D,--option1 option above.

The argc, and argv parameters are the parameters of the main function. optstring points out the parameters we can accept. Its general form is: Parameter 1[:] parameter 2[:] .... Where the parameter is a parameter that we can accept, if the following colon is not omitted, then the parameter value must be followed when the parameter appears. For example, a optstring is abc:d: Indicates that this parameter option can be a,b,c,d where c,d must have a parameter value when it appears. If we enter a parameter option that we have not provided. The system will say unfamiliar options. GETOPT returns the parameter options we specified. Also save the parameter values in Optarg, if the analysis completes all the parameter functions return-1. This time optind indicates the start position of the non optional parameter.

#include <stdio.h> #include <unistd.h> int main (int argc,char **argv) {int is_a,is_b,is_c,is_d,i; char *a_ Value,*b_value,*c_value,temp;   Is_a=is_b=is_c=is_d=0;   A_value=b_value=c_value=null; if (argc==1) {fprintf (stderr, usage:%s [-a value] [-B-value] [-c-value] [-d] arglist ...   ", Argv[0]);   Exit (1);   while (Temp=getopt (ARGC,ARGV, a:b:c:d))!=-1) {switch (temp) {case ' a ': is_a=1;   A_value=optarg;   Break   Case ' B ': is_b=1;   B_value=optarg;   Break   Case ' C ': is_c=1;   C_value=optarg;   Break   Case ' d ': is_d=1;   Break } printf ("option has a:%s with value:%s", is_a?)   Yes ":" NO ", A_value); printf ("option has b:%s with value:%s", Is_b?)   Yes ":" NO ", B_value); printf ("option has c:%s with value:%s", Is_c?)   Yes ":" NO ", C_value); printf ("option has d:%s", is_d?)   Yes ":" NO ");   I=optind;   while (Argv[i]) printf (' With arg:%s ', argv[i++]); Exit (0); }

Getopt_long is a bit more complicated than getopt, but it's more versatile than getopt. Struct option indicates the additional parameter options we can accept.
Name: Indicates the names of long options (such as our option1)

Has_flag: 0 means that there is no parameter value, when 1, this parameter option to accept a parameter value. For 2, the parameter value may or may not be.
Indicates the return value of the function. If NULL, return Val, or return 0. and assign Longindex to the location of the array (longopts) where the option is located.

 /* This example is from the GNU Libc manual * * #include <stdio.h> #include <stdlib.h> #include <getopt.h>      int main (int argc, char **argv) {int C; while (1) {struct option long_options[] = {{"Add", 1, 0, 0}, {"Append", 0, 0, 0}, {"Delete", 1, 0, 0},/*   Returns the character C, equivalent to the-C option */{"Create", 0, 0, ' C '}, {"File", 1, 0, 0},/* array end */{0, 0, 0, 0}}; /* Getopt_long stores the option index here.      */int option_index = 0;      c = Getopt_long (argc, argv, "Abc:d:", Long_options, &option_index); /* Detect the end of the options.      * * if (c = = 1) break;   Switch (c) {case 0:printf ("option%s", long_options[option_index].name);   if (optarg) printf ("With arg%s", Optarg);      Break   Case ' a ': Puts ("option-a");      Break   Case ' B ': Puts ("option-b");      Break   /* May be the-C--creat parameter refers to the/case ' C ': printf ("Option-c with value '%s '", Optarg);      Break Case ' d ': printf ("option-d withValue '%s ', optarg);   Break   } exit (0); }  

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.