以前学C语言的时候,很少用到int argc,char *argv[],感觉没啥用,所以也就没怎么了解。现在用到了,来学习一下。argc命令行输入参数的数量,argv是字符数组,看代码。
[mapan@localhost mapam]$ ls
test.cpp
[mapan@localhost mapam]$ g++ test.cpp
[mapan@localhost mapam]$ cat test.cpp
#include<stdio.h>
int main(int argc,char *argv[])
{
printf("argv[0]=%s\n",argv[0]);
//printf("argv[1]=%s\n",argv[1]);
printf("argv[2]: %s\n",argv[2]);
printf("argv[3]: %s\n",argv[3]);
printf("argv[4]: %s\n",argv[4]);
printf("argv[5]: %s\n",argv[5]);
printf("argv[6]: %s\n",argv[6]);
printf("argv[7]: %s\n",argv[7]);
printf("argv[8]: %s\n",argv[8]);
printf("argv[9]: %s\n",argv[9]);
printf("argv[10]: %s\n",argv[10]);
return 0;
}
[mapan@localhost mapam]$ ./a.out
argv[0]=./a.out
argv[2]: HOSTNAME=localhost.localdomain
argv[3]: TERM=xterm
argv[4]: SHELL=/bin/bash
argv[5]: HISTSIZE=1000
argv[6]: SSH_CLIENT=192.168.1.51 13559 22
argv[7]: QTDIR=/usr/lib64/qt-3.3
argv[8]: OLDPWD=/home/mapan/test
argv[9]: QTINC=/usr/lib64/qt-3.3/include
argv[10]: SSH_TTY=/dev/pts/1
[mapan@localhost mapam]$
[mapan@localhost mapam]$ ./a.out a b c d
argv[0]=./a.out
argv[2]: b
argv[3]: c
argv[4]: d
argv[5]: (null)
argv[6]: HOSTNAME=localhost.localdomain
argv[7]: TERM=xterm
argv[8]: SHELL=/bin/bash
argv[9]: HISTSIZE=1000
argv[10]: SSH_CLIENT=192.168.1.51 13559 22
argv[1]应该是a
今天遇到的问题,找到原因发现也就这点东西。