MarkdownPad Document
LinuxC下argc,argv[ ]的意义
参考了http://blog.csdn.net/followingturing/article/details/7707584
还是根据实例来进行说明:
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main(int argc,char *argv[]) 5 { 6 if(argc==1 || argc>2) 7 { 8 printf("Please input the name of the file 9 you want to edit, for example:./edit fillen "); 10 printf("argv[0] is %s ",argv[0]); 11 } 12 13 if(argc==2) 14 { 15 printf("Now EDIT %s ",argv[1]); 16 printf("argv[0] is %s ",argv[0]); 17 printf("argv[1] is %s ",argv[1]); 18 } 19 20 exit(0); 21 }
- 通过gcc生成可执行文件edit如下:
- 运行文件edit:./edit,如下:
- 运行文件edit,并输入file.txt: ./edit file.txt,如下:
结论:
- argc 为外部命令参数的个数,因此
输入命令./edit时,argc==1;
输入命令./edit file.txt时,argc==2; - argv[]为存储的命令,其中argv[0]即为可执行文件名;
输入命令./edit时,argv[0]=="./edit";
输入命令./edit file.txt时,argv[0]=="./edit",argv[1]=="file.txt";
2017-2-4 23:10;20