O***e 发帖数: 89 | 1 I know the meaning of argc and argv, but why argc=3 is required in many
programs, the following is an example.
#int main(int argc, char * argv[])
#{
#char chline[LINESIZE+1];
#const char * self=argv[0];
#const char * ipfile=argv[1];
#const char * opfile=argv[2];
#if ( ! (argc==3))
#{
#if (argc<3)
#cout<<"\n\nerror: too few arguments"<
#if (argc>3)
#cout<<"\n\nerror: too many parameters"< |
p**s 发帖数: 2707 | 2 I guess 3 is a magic number
【在 O***e 的大作中提到】 : I know the meaning of argc and argv, but why argc=3 is required in many : programs, the following is an example. : #int main(int argc, char * argv[]) : #{ : #char chline[LINESIZE+1]; : #const char * self=argv[0]; : #const char * ipfile=argv[1]; : #const char * opfile=argv[2]; : #if ( ! (argc==3)) : #{
|
S*********g 发帖数: 5298 | 3 No, you don't know the meaning of argc.
【在 O***e 的大作中提到】 : I know the meaning of argc and argv, but why argc=3 is required in many : programs, the following is an example. : #int main(int argc, char * argv[]) : #{ : #char chline[LINESIZE+1]; : #const char * self=argv[0]; : #const char * ipfile=argv[1]; : #const char * opfile=argv[2]; : #if ( ! (argc==3)) : #{
|
O***e 发帖数: 89 | 4 argc 包含命令行选项的个数argv 包含aygc 个C 风格字符串代表了由空格分隔的命令
选项例如对于如下命令行
prog -d -o ofile data0
argc 被设置为5
I am sure I missed something, if you know, tell me and thanks!
【在 S*********g 的大作中提到】 : No, you don't know the meaning of argc.
|
r****t 发帖数: 10904 | 5 the code says
./self ipfile opfile
so argc>=3. It is a bad UI design, though.
【在 O***e 的大作中提到】 : I know the meaning of argc and argv, but why argc=3 is required in many : programs, the following is an example. : #int main(int argc, char * argv[]) : #{ : #char chline[LINESIZE+1]; : #const char * self=argv[0]; : #const char * ipfile=argv[1]; : #const char * opfile=argv[2]; : #if ( ! (argc==3)) : #{
|
k**f 发帖数: 372 | 6
argc includes the program name. So argv[0] is prog in your example. With
that you have 5 command line parameters for your program.
In your earlier post, the programs you encountered happened to use 3
additional parameters. There are plenty of programs using other number of
parameters, too.
【在 O***e 的大作中提到】 : argc 包含命令行选项的个数argv 包含aygc 个C 风格字符串代表了由空格分隔的命令 : 选项例如对于如下命令行 : prog -d -o ofile data0 : argc 被设置为5 : I am sure I missed something, if you know, tell me and thanks!
|
O***e 发帖数: 89 | 7 THanks replies very much, it help much and I roughly get it. |
T*****9 发帖数: 2484 | 8 不就是self ipfile opfile
【在 O***e 的大作中提到】 : I know the meaning of argc and argv, but why argc=3 is required in many : programs, the following is an example. : #int main(int argc, char * argv[]) : #{ : #char chline[LINESIZE+1]; : #const char * self=argv[0]; : #const char * ipfile=argv[1]; : #const char * opfile=argv[2]; : #if ( ! (argc==3)) : #{
|
E*****7 发帖数: 128 | 9 #if ( ! (argc==3))
这是测试在命令行执行该文件时,文件名的后面是不是带有两个参数:
C:\ExeThisCode para_1 para_2
后面:
#if (argc<3)
#cout<<"\n\nerror: too few arguments"<
#if (argc>3)
#cout<<"\n\nerror: too many parameters"<
程序员要求在命令行执行该文件时文件名的后面必须带有两个参数,否则给出错信
息(参数太少,参数太多)。 |
O***e 发帖数: 89 | 10 I get it, thanks all guys. |
l*****d 发帖数: 359 | 11 magic number is 3, command_name, input_name, out_putname, so that adds up to
three. |
l*****m 发帖数: 772 | |