2014-02-20 17 views

回答

2
#include <string.h> 
int main(int argc, char *argv[]) 
{ 
    int i; 
    for (i = 1; i < argc; ++i) 
    { 
     if (argv[i][0] == '-' && argv[i][1] == 'o') 
     { 
      /* "-o" detected. Take care of argv[i] here. */ 
     } 
    } 
} 
1

使用getopt的

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <getopt.h> 

int main(int argc , char *argv[]) { 
    char *file; 
    int opt; 
    while ((opt = getopt(argc, argv, "o:")) != -1){ 
     switch(opt){ 
     case 'o': 
      file=strdup(optarg); 
      printf("file is %s\n", file); 
      free(file); 
      break; 
     default : 
      ;//return 1; 
     } 
    } 
    return 0; 
} 
/* demo 
>a.out -o test.c 
file is test.c 
*/ 
相關問題