Chinaunix首页 | 论坛 | 博客
  • 博客访问: 102970
  • 博文数量: 24
  • 博客积分: 105
  • 博客等级: 民兵
  • 技术积分: 244
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-09 20:05
文章分类

全部博文(24)

文章存档

2015年(1)

2014年(9)

2013年(10)

2012年(4)

我的朋友

分类: LINUX

2012-02-13 22:24:27

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/unistd.h>
  1. int main(int argc, char * argv[])
  2. {
  3.         int opt;
  4.         char * name = NULL;
  5.         while (-1 != (opt = getopt(argc, argv, "n:lm"))) {
  6.                 switch (opt) {
  7.                         case 'n':
  8.                         name = strdup(optarg);
  9.                         if (NULL == name) {
  10.                                 fprintf(stderr, "memory is exhausted!\n");
  11.                                 exit(1);
  12.                         }
  13.                         break;
  14.                         case 'l':
  15.                         case 'm':
  16.                         break;
  17.                         default:
  18.                         fprintf(stderr, "usage: %s [options] [optarg]\n", argv[0]);
  19.                         break;
  20.                 }
  21.         }
  22.                                                                                
  23.         if (name) {
  24.                 fprintf(stdout, "the optarg is: %s\n", name);
  25.                 free(name);
  26.         }
  27.                                                                              
  28.         return 0;
  29. }
  30. 格式化字符串中字符‘:’前需要带选项参数
阅读(856) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~