Linux辅导:用Getopt在Linux下获取命令行参数方法
来源:优易学  2011-1-15 16:35:38   【优易学:中国教育考试门户网】   资料下载   IT书店

  2、例子程序
 
  下面是一个使用getopt函数读取参数的例子程序,该程序支持 –p –s –b –c的参数,并将这些参数值读取后进行打印。你可以根据需要将这些代码稍作修改用于自己的程序中。
 
  

/*

* getopt.c

* Author: Coonxu

* email: coonxu@126.com

* 这是一个使用getopt函数读取参数的例子程序,该程序支持 -p -s -b -c的参数,

* 并将这些参数值读取后进行打印。你可以根据需要将这些代码稍作修改用于自己的程序中。

*/

#include 

#include 

int main(int argc, char **argv)

{

int ch;

opterr = 0;

while( ( ch = getopt( argc, argv, "s:b:c:p:" ) ) != EOF )

{

switch(ch)

{

case 's':

printf("s opt: %s\n", optarg);

break;

case 'b':

printf("b opt: %s\n", optarg);

break;

case 'c':

printf("c opt: %s\n", optarg);

break;

case 'p':

printf("p opt: %s\n", optarg);

break;

case '?':

printf( "illegal option: %c\n", ch );

break;

} 

}



return 0;

}

上一页  [1] [2] 

责任编辑:虫虫

文章搜索:
 相关文章
热点资讯
热门课程培训