getopt():用來解析命令行參數(shù)
#include<unistd.h> extern char * optarg; extern int optind; extern int opterro; extern int optopt;getopt(int argc, char ** argv, char *options)
按照字符串options提供的選項在參數(shù)列表argv中解析出選項。 注:選項和參數(shù) 1. 單一字符是選項 2. 字符后面加:表示選項后需要加一個參數(shù)(隔一個空格或直接跟在后面) 3. 字符后面加兩個::,表示緊接著需要一個參數(shù)
eg:
getopt(argc, argv, "ab:c:de::")執(zhí)行:./main file1 -a -b -c code -d file2 -e file3結(jié)果:option : a optarg : (null) opterror : 1 optind : 3option : b optarg : -c opterror : 1 optind : 5option : d optarg : (null) opterror : 1 optind : 7option : e optarg : (null) opterror : 1 optind : 9argv變成:./main -a -b -c -d -e file1 code file2 file3新聞熱點
疑難解答