int mian( int argc, char ** argv) 命令行参数初始化?
如何从命令行获取参数,比如,在Linux命令窗口输入如下指令: programname key1=value, key2=value2.... 程序programname便能从命令行获取参数,而这通常需要参数初始化。如下,就是一个参数初始化的程序,不是很明白,哪位大侠能指点一二,不甚感激! 程序代码:
/* parameter table */typedef struct { char *name; /* external name of parameter */ char *asciival; /* ascii value of parameter */} pointer_table; extern int xargc; extern char **xargv; /* global variables declared and used internally */static pointer_table *argtbl; /* parameter table */static int nargs; /* number of args that parse */static int tabled = FALSE; /* true when parameters tabled */static size_t targc; /* total number of args */static char **targv; /* pointer to arg strings */static char *argstr; /* storage for command line */ /* functions declared and used internally */static int getparindex (int n, char *name);static void getparinit(void);static void tabulate (size_t argc, char **argv);static char *getpfname (void);#if 0/* getpars.c:571: warning: `white2null' defined but not used *//* not used */static size_t white2null (char *str, size_t len);#endifstatic int ccount (char c, char *s); /*--------------------------------------------------------------------*\ These variables are used by checkpars() to warn of parameter typos. par= does not use getpar() so we need to store that as the first parameter name. lheaders= is buried in fgettr() so we intialize that also\*--------------------------------------------------------------------*/ #define PAR_NAMES_MAX 512 static char* par_names[PAR_NAMES_MAX];static int par_count=0;static int parcheck = 0; /* make command line args available to subroutines -- re-entrant version */void initargs(int argc, char **argv){ memset( par_names ,0 ,sizeof(par_names) ); par_names[0] = "par"; par_names[1] = "lheader"; par_count=2; xargc = argc; xargv = argv; if(tabled==TRUE){ free(argstr); free(targv); free(argtbl); } tabled = FALSE; return;} . 分享到: