代写command-line、代做C/C++编程、代做arbitrary program、c++代写代做R语言程序|帮做C/C++编程

2019/1/29 Working with getopt()https://cis.technikum-wien.at/documents/bel/1/prg/semesterplan/tasks/content/getopt.html 1/3Working with getopt()The function getopt() allows to implement programs with command-line arguments in amore flexible manner. In particular, it releaves the user of the program from remebering thecorrect order of the arguments and whether arguments are optional or mandatory. Thecommand-line arguments need to be pre-fixed with an additional - argument, e.g., -iargument .The following usage example shows an arbitrary program:The first example prints a help message - we use only -h without additional argument.$ prog.exe -hNext, we use the program with four arguments - three with an additional arguments followingthe -i , -o , and -p , respectively. The fourth argument -r doesn’t request an additionalargument.$ prog.exe -i infile.txt -o ofile.txt -p 10 -rThe third example is the same as the one before, however, with a different order of thearguments.$ prog.exe -p 10 -o ofile.txt -r -i infile.txtUsing getopt()The following listing shows how to use getopt():2019/1/29 Working with getopt()https://cis.technikum-wien.at/documents/bel/1/prg/semesterplan/tasks/content/getopt.html 2/3#include #include #include #include #include #include void print_help (void);int main (int argc, char *argv[]){int opt;int parg = INT_MIN;_Bool rarg = false;char *iarg = ;char *oarg = ;// no arguments givenif (argc == 1) {fprintf (stderr, This program needs arguments....\n\n);print_help();}// use getopt to evaluate the parameterswhile ( (opt = getopt (argc, argv, i:o:p:rh)) != -1) {switch (o代写command-line作业、代做C/C++编程作业、代做arbitrary program作业、c++实验作业代写pt) {case i:iarg = optarg;break;case o:oarg = optarg;break;case p:sscanf (optarg, %d, &parg);break;case r:rarg = true;break;case h:print_help();break;case ::fprintf (stderr, Option requires an argument.\n);print_help ();break;case ?:fprintf (stderr, Unknown option character %c.\n, optopt);print_help ();}}for (; optind printf (Positional argument %d: %s\n, optind, argv[optind]);// now we print the parameters we have gotif (strcmp (iarg, ) != 0)printf (The program %s was invoked with -i %s\n, argv[0], iarg);C2019/1/29 Working with getopt()https://cis.technikum-wien.at/documents/bel/1/prg/semesterplan/tasks/content/getopt.html 3/3The getopt() function gets the arguments listed as a string. Every - argument is listed bythe respective letter. When one of these - arguments requests an additional argument acolon is added after the respective letter; in the above example the arguments -i, -o, and -prequest an additional argument. Note - only one additional argument can be requested.For every argument add a respective case statement. Often it is advisible to copy theadditional argument to a variable for post processing after the switch statement.if (strcmp (oarg, ) != 0)printf (The program %s was invoked with -o %s\n, argv[0], oarg);if (parg != INT_MIN)printf (The program %s was invoked with -p %d\n, argv[0], parg);if (rarg != false)printf (The program %s was invoked with -r\n, argv[0]);return 0;}voidprint_help (void){printf (\nSyntax: prog.exe [-i infile] [-o outfile] [-p value] [-r] [-h]\n);exit (EXIT_FAILURE);}Last updated 2018-08-22 14:44:10 CEST转自:http://ass.3daixie.com/2019013117233330.html

你可能感兴趣的:(代写command-line、代做C/C++编程、代做arbitrary program、c++代写代做R语言程序|帮做C/C++编程)