命令行参数

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  
 5 int main(int argc, char *argv[], char *env[])  
 6  {  
 7      int i;  
 8      printf("These are the %d command- line arguments passed to main:\n\n", argc);      
 9      for(i=0; i<=argc; i++)
10     {
11         printf("argv[%d]:%s\n", i, argv[i]); 
12     }
13     
14      printf("\nThe environment string(s)on this system are:\n\n");      
15      for(i=0; env[i]!=NULL; i++)  
16      {
17         printf(" env[%d]:%s\n", i, env[i]);  //C++ Code: cout<< "argv[" << i <<"]" << argv[i] <<endl;
18      }
19      
20      return 0;
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>

using namespace std;
int main( int argc,      // Number of strings in array argv
          char *argv[],   // Array of command-line argument strings
          char *envp[] )  // Array of environment variable strings
{
    
int count;

    
// 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 Lines
Command-Line Input      argv[1]      argv[2]         argv[3]  
  "abc" d e abc d E
  
a\\\b d"e f"g h a\\\b de fg H

a\\\"b c d a\"b c D

a\\\\"b c" d e a\\b c d E
 
posted @ 2010-08-23 17:09  red_giser  阅读(1882)  评论(0编辑  收藏  举报