C++ 命令行启动的参数传递

命令行启动时,经常需要把参数传递到程序里,代码如下:

#include <iostream>

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

	for (int i = 0; i < argc; i++) {
		std::cout << "argument " << i << " = " << argv[i] << std::endl;
	}

	return 0;
}

编译好之后,在工程生成的目录下,如这个功能我命名为TestCpp,生成目录下:TestCpp.exe,新建启动脚本 ,test_cmd.bat

chcp 65001
TestCpp.exe "hello" "world"
pause

最后就可以把"hello" "world"两个参数传递到程序里,方便启动时需要读取一些路径或其它外部参数

发布了70 篇原创文章 · 获赞 48 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/fwb330198372/article/details/82345633