int main(int argc, char *argv[]) { ... }

来源:百度知道 编辑:UC知道 时间:2024/06/19 23:56:39
//e:/c++/test/debug/test.exe
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
int i;
for (i = 0; i<argc; i++)
cout<<argv[i]<<endl;
return 0;
}
这是一个简单的程序,为什么运行以后直接显示e:/c++/test/debug/test.exe呢?

不是应该手动输入argument的吗?然后再显示的说输入的数据吗?
我用的是vs2005.
参数1,参数2格式怎么写啊? 比如我想调用e盘里文件夹1中的m.txt, 这样写:e:/文件夹1/m.txt 怎么不行啊,是不能写汉字呢,还是路径写的不对啊?希望高手指点我一下,万分感激哦

#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
int i;
cout<<argc<<endl;
for (i = 0; i<argc; i++)
cout<<argv[i]<<endl;
return 0;
}

cmd 中这样运行(当然先要compile和build)
F:\c++\Debug/longTimeNoSee.exe hello world
你看看这个就明白了
当你输入hello world 的时候你会看到
argc的值是3,当你什么也不输入的时候
argc的值是1,路径也作为一个参数加到argc中的。

argc = 1, argv[0] :e:/c++/test/debug/test.exe

这个是默认的,不管你有没有输入参数,这个都是第一个,默认的

argc:表示参数个数,argv 保存所有参数
需要注意的是,程序本身路径永远是第一参数。
所以直接运行相当于在CMD下敲命令:
e:/c++/test/debug/test.exe 回车执行
这时:argc = 1, argv[0] = "e:/c++/test/debug/test.exe"
CMD使用:
例子1:
E:\>e:/c++/test/debug/test.exe 参数1 参数2 回车执行
argc = 3, argv[0] = "e:/c++/test/debug/test.exe"
argv[1] = "参数1" argv[2] = "参数2"
例子2:
E:\C++\test\debug\>test.exe 参数1 参数2 回车执行<