写一个要使用到命令行的程序test.c
#include <stdio.h>
#include <unistd.h>
int main(int argc, char* args[])
{
int thick = 0;
int count = 0;
char ch;
while ((ch = getopt(argc, args, "t")) != EOF)
switch (ch) {
case 't':
thick = 1;
break;
default:
fprintf(stderr, "Unknown option: '%s'\n", optarg);
return 1;
}
printf("optind:%d\n", optind);
printf("argc:%d\n", argc);
// 结束while循环后,查找其它参数,
// 为了跳过已经读取的参数,需要argc -= optind;args += optind;
argc -= optind;
printf("argc:%d\n", argc);
printf("args before:%s\n", *args);
args += optind;
printf("args after:%s\n", *args);
if (thick)
puts("Thick crust.");
puts("Other args:");
for (count = 0; count < argc; count++)
puts(args[count]);
retu