CPP中主函数的参数

 CPP中主函数的参数大部分书中没有介绍,本文详细介绍其中的内容,并举以实例,以飨访友。
Arguments to main()
1 For CPP
You can define main() with no parameters (or better, with the parameter list as void) or you can specify
a parameter list that allows the main() function to obtain values from the command line from the execute
command for the program. Values passed from the command line as arguments to main() are always
interpreted as strings. If you want to get data into main() from the command line, you must define it
like this:
int main(int argc, char* argv[])
{
// Code for main()...
}
The first parameter is the count of the number strings found on the command line including the program
name, and the second parameter is an array that contains pointers to these strings plus an additional
element that is null. Thus argc is always at least 1 because you at least must enter the name of the
program. The number of arguments received depends on what you enter on the command line to execute
the program. For example, suppose that you execute the DoThat program with the command:
DoThat.exe
There is just the name of the .exe file for the program so argc is 1 and the argv array contains two
elements—argv[0] pointing to the string “DoThat.exe” and argv[1] that contains null.
Suppose you enter this on the command line:
DoThat or else “my friend” 999.9
Now argc is 5 and argv contains six elements, the last element being 0 and the first 5 pointing to the
strings:
“DoThat” “or” “else” “my friend” “999.9”
You can see from this that if you want to have a string that includes spaces received as a single string
you must enclose it between double quotes. You can also see that numerical values are read as strings so
if you want conversion to the numerical value that is up to you.
Let’s see it working.
Try It Out Receiving Command-Line Arguments
This program just lists the arguments it receives from the command line.

// Reading command line arguments
#include <iostream>
using std::cout;
using std::endl;
int main(int argc, char* argv[])
{
cout << endl << “argc = “ << argc << endl;
cout << “Command line arguments received are:” << endl;
for(int i = 0 ; i <argc ; i++)
cout << “argument “ << (i+1) << “: “ << argv[i] << endl;
return 0;
}

You have two choices when entering the command-line arguments. After you build the example in the
IDE, you can open a command window at the folder containing the .exe file and then enter the program
name followed by the command-line arguments. Alternatively, you can specify the command-line
arguments in the IDE before you execute the program. Just open the project properties window by
selecting Project > Properties from the main menu and then extend the Configuration
Properties tree in the left pane by clicking the plus sign (+). Click the Debugging folder to see where
you can enter command line values in the right pane.
Here is some typical output from this example entered in a command window:
C:/Visual C++ 2005/Examples/Ex5_09 trying multiple “argument values” 4.5 0.0
argc = 6
Command line arguments received are:
argument 1: Ex5_09
argument 2: trying
argument 3: multiple
argument 4: argument values
argument 5: 4.5
argument 6: 0.0
How It Works
The program first output the value of argc and then the values of each argument from the argv array in
the for loop. You could make use of the fact that the last element in argv is null and code the output of
the command line argument values like this:
int i = 0;
while(argv[i] != 0)
cout << “argument “ << (i+1) << “: “ << argv[i++] << endl;
The while loop ends when argv[argc] is reached because that elment is null.

2 For C++/CLI
There is only one parameter to the main() function in aC++/CLI program and it is an array of elements of type String^. Accessing and processing commandline arguments in a C++/CLI program boils down to just accessing the elements in the array parameter.
You can try it out.
Try It Out Accessing Command Line Arguments
Here’s a C++/CLI version of Ex5_09.
// .cpp : main project file.
// Receiving multiple command liner arguments.
#include “stdafx.h”
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L”There were {0} command line arguments.”,
args->Length);
Console::WriteLine(L”Command line arguments received are:”);
int i = 1;
for each(String^ str in args)
Console::WriteLine(L”Argument {0}: {1}”, i++, str);
return 0;
}
You can enter the command-line arguments in the command window or through the project properties
window as described earlier in the chapter. This example produces the following output:
C:/Visual C++ 2005/Examples/>Ex5_16 trying multiple “argument values” 4.5 0.0
There were 5 command line arguments.
Command line arguments received are:

Argument 1: trying
Argument 2: multiple
Argument 3: argument values
Argument 4: 4.5
Argument 5: 0.0
How It Works
From the output, you can see that one difference between this and the native C++ version is that you
don’t get the program name passed to main()—not really a great disadvantage, really a positive feature
in most circumstances. Accessing the command line arguments is now a trivial exercise involving just
iterating through the elements in the args array.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值