c语言中命令行参数argc,argv
main(int argc,char argv)argv为指针的指针argc为整数 表示命令行参数的个数
char argv or: char argv[] or: char argv[][]
main()括号内是固定的写法。
下面给出一个例子来理解这两个参数的用法:
假设程序的名称为Command_Line.exe,完整路径名为:C:\Command_Line.exe
当只输入C:\Command_Line.exe 则由操作系统传来的参数为:argc=1,表示只有一程序名称。char argc[]只有一个元素,argv[0]指向输入的程序路径及名称:Command_Line.exe当输入的参数为:
"localhost" "esri_sde" "oradb" "zhangbinglong" "zbl" "default"
则argc = 7;其中:argv[1] = localhost;argv[2] = esri_sde;......argv[6] = default;
使用命令行参数有两种方式:1.项目属性->配置属性->调试->命令参数:输入:"localhost" "esri_sde" "oradb" "zhangbinglong" "zbl" "default"可以使用双引号标识, 也可以只保留空格,具体可参见msdn.2.Dos窗口下执行程序:C:\Command_Line.exe "localhost" "esri_sde" "oradb" "zhangbinglong" "zbl" "default"->enter...............................
请注意: 一旦想说明这些参数, 则必须按argc, argv, env 的顺序, 如以下的例子:main()main(int argc)main(int argc, char argv[])main(int argc, char argv[], char env[])其中第二种情况是合法的, 但不常见, 因为在程序中很少有只用argc, 而不用argv[]的情况。以下提供一样例程序EXAMPLE.EXE, 演示如何在main()函数中使用三个参数:1/program name EXAMPLE.EXE/2#include<stdio.h>
3#include<stdlib.h>
4
5intmain(intargc,charargv[],char*env[])
6{
7inti;
8printf("These are the %d command- line arguments passed to main:\n\n", argc);
9for(i=0; i<=argc; i++)
10{
11printf("argv[%d]:%s\n", i, argv[i]);
12}
13
14printf("\nThe environment string(s)on this system are:\n\n");
15for(i=0; env[i]!=NULL; i++)
16{
17printf("env[%d]:%s\n", i, env[i]);//C++ Code: cout<< "argv[" << i <<"]" << argv[i] <<endl;
18}
19
20return0;
21
22}
应该提醒的是: 传送main() 函数的命令行参数的最大长度为128 个字符 (包括参数间的空格), 这是由DOS 限制的。
Parsing C++ Command-Line Arguments :
Microsoft C/C++ startup code uses the following rules when interpreting arguments given on the operating system command line:
1.Arguments are delimited by white space, which is either a space or a tab.
2.The caret character (^) is not recognized as an escape character or delimiter. The character is handled completely by the command-line parser in the operating system before being passed to the argv array in the program.
3.A string surrounded by double quotation marks ("string") is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument.
4.A double quotation mark preceded by a backslash (\") is interpreted as a literal double quotation mark character (" ).
5.Backslashes are interpreted literally, unless they immediately precede a double quotation mark.
6.If an even number of backslashes is followed by a double quotation mark, one backslash is placed in the argv array for every pair of backslashes, and the double quotation mark is interpreted as a string delimiter.
7.If an odd number of backslashes is followed by a double quotation mark, one backslash is placed in the argv array for every pair of backslashes, and the double quotation mark is "escaped" by the remaining backslash, causing a literal double quotation mark (") to be placed in argv.
Example:The following program demonstrates how command-line arguments are passed://command_line_arguments.cpp
//compile with: /EHsc
#include<iostream>
usingnamespacestd;
intmain(intargc,//Number of strings in array argv
charargv[],//Array of command-line argument strings
charenvp[] )//Array of environment variable strings
{
intcount;
//Display each command-line argument.
cout<<"\nCommand-line arguments:\n";
for( count=0; count<argc; count++)
cout<<"argv["<<count<<"]"
<<argv[count]<<"\n";
}
The following table shows example input and expected output, demonstrating the rules in the preceding list.
Results of Parsing Command LinesCommand-Line Input argv[1] argv[2] argv[3]"abc" d eabcdEa\\b d"e f"g ha\\bde fgH
a\\"b c da\"bcD
a\\"b c" d ea\b cdE原文链接: https://www.cnblogs.com/red_giser/archive/2010/08/23/1806628.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/14226
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!