他们在做什么? if(argc <2)或if(argc!= 2)条件和程序中的argv [1]? [英] What do they do? if(argc<2) or if(argc!=2) conditions and argv[1] in programs?
问题描述
您好!
我在许多程序中看到他们使用
Hello!
I've seen in many programs that they use
if(argc!=2)
或
if(argc<2)
条件,为什么以及何时使用这些条件?
我也见过很多程序他们写的argv [1]喜欢
conditions, why and when they use this conditions?
Also I've seen in many programs they write argv[1]like
if(argv[1])
或者程序中的其他地方。
当我写
or other places in a program.
When I write
cout<<argv[1];
在我的程序中,我没有结果!我不知道为什么?而且当我在我的程序中编写argv [1]时,我会遇到逻辑错误!
但是当我写
in my programs, I get no result! I dont know why? and aalso when I write argv[1] in my programs I get logical errors!
But when I write
cout<<argv[0];
我得到的路径类似于
/home/user/program_folder/program_name/bin/program_name/Debug/program_name
。
你能解释我何时以及如何使用argc和argv参数?
谢谢
.
Can you explain when and how can I use argc and argv arguments?
Thank you
推荐答案
我想每本书关于 C
编程语言解释了这一点。无论如何,我会 Google for [ ^ ]。
I suppose every book about the C
programming language explains that. Anyway I would Google for[^].
argc
和argv
是传递的命令行字符串的两部分如果它是从命令行启动到你的应用程序,或者从另一个应用程序中作为进程启动时给出选项arguemnets。
例如,如果你的应用是注册为处理扩展名为.MyAppFiles的所有文件,并且用户在Windows资源管理器中双击MyFile.MyAppFiles,您的应用程序将启动并给出文件名:
argc
andargv
are the two parts of the command line string which are passed to your application if it is started from the command line, or given optiona arguemnets when started as a process from within a different application.
For example, if your app is registered as handling all files with an extension ".MyAppFiles" and the user double clicks on "MyFile.MyAppFiles" in Windows Explorer, you app will be started and given the file name:
C:\MyPath\MyFile.MyAppFiles
作为命令行参数 - 所以 argc
将是计数,并且 argv
将包含一个字符串数组(或者更确切地说是 char **
),其中包含实际数据。 />
如果有足够的参数可以继续并安全地完成工作,那么您正在查看的所有代码都在运行。
as a command line argument - so argc
will be the count, and argv
will be an array of strings (or more accurately a char**
) which contains the actual data.
All the code you are looking at is doing is working out if it has enough parameters to continue and do its job in safety.
argc
是argv
中参数数量的计数,并且是1以上的任何数字。第一个参数argv [0]
始终是可执行程序的路径,例如
argc
is the count of the number of parameters inargv
and will be any number from 1 upwards. The first parameterargv[0]
is always the path to the executable program, e.g.
/home/user/program_folder/program_name/bin/program_name/Debug/program_name
其余指针是在命令行上传递的参数,或者当用户通过double启动程序时由Explorer传递的参数单击文件名。因此,如果您从上述路径调用该程序,例如:
The remaining pointers are the parameters passed on the command line, or by Explorer when the user starts the program by double clicking a file name. So if you call the program from the above path like:
program_name foo bar "A long string of nonsense"
argc
将等于4, argv
值将为:
argc
will equal 4, and argv
values will be:
argv[0] : /home/user/program_folder/program_name/bin/program_name/Debug/program_name
argv[1] : foo
argv[2] : bar
argv[3] : A long string of nonsense
这篇关于他们在做什么? if(argc <2)或if(argc!= 2)条件和程序中的argv [1]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!