话不多说,先上代码
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
int main(int argc, char *argv[])
{
FILE *file;
int opt;
char *optstring = "a:b:c:d";
char fname[50] ;
while ((opt = getopt(argc, argv, optstring)) != -1)
{
switch(opt)
{
case 'a':
strcpy(fname,optarg);
if(mkdir(fname,0777)==-1)
printf("fail to build directionary\n");
// if((file=fopen(fname,"w+"))==NULL)
// printf("open file error\n");
break;
}
}
return 0;
}
下面是测试(我的.c文件名位get_option.c):
同理,将代码中创建文件夹的指令换成穿件文件,即可达到相同的效果。