#include
#include
#include
int main(int argc,char *argv[])
{
int opt;
while((opt=getopt(argc,argv,"n:"))!=-1)
{
printf("%s\n",argv[optind]);
}
比如
./test 3-n5 4 -n4 5 -n3 我想输出3 4 5怎么做比较好,谢谢各位!
#include
#include
#include
int main(int argc,char *argv[])
{
int opt;
while((opt=getopt(argc,argv,"n:"))!=-1)
{
printf("%s\n",argv[optind]);
}
比如
./test 3-n5 4 -n4 5 -n3 我想输出3 4 5怎么做比较好,谢谢各位!
int index = 0;
int my_getopt(int argc, char *argv[], char *optstring)
{
char c;
int tind=0;
index ++;
if(index>=argc)
return -1;
if(argv[index][0]!='-')
{
return 1;
}
c = argv[index][1];
while(optstring[tind] && optstring[tind]!=c)
tind++;
if(!optstring[tind])
return -1;
index += (optstring[tind+1]==':');
return my_getopt(argc, argv, optstring);
}
int main(int argc,char *argv[])
{
while(my_getopt(argc,argv,"n:")!=-1)
{
printf("%s\n",argv[index]);
}
}