先看程序:
- #include<iostream>
- using namespace std;
-
- int main(int argc, char *argv[])
- {
- cout << argc << endl;
- cout << argv[0] << endl;
-
- return 0;
- }
结果为:
1
C:\Documents and Settings\Administrator\桌面\cpp\Debug\test.exe
在VC6.0中选择“工程”,设置,然后按下面的图片填上相关信息:
此时,将程序改为:
- #include<iostream>
- using namespace std;
-
- int main(int argc, char *argv[])
- {
- cout << argc << endl;
- cout << argv[0] << endl;
- cout << argv[1] << endl;
- cout << argv[2] << endl;
- cout << argv[3] << endl;
-
- return 0;
- }
结果为:
4
C:\Documents and Settings\Administrator\桌面\cpp\Debug\test.exe
a
b
c
如果程序要读取某文件的信息,而该文件的名称不是在程序中给定,而是由用户确定,那么给main传参数就是一个很好的方法.