命令行啓動時,經常需要把參數傳遞到程序裏,代碼如下:
#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"兩個參數傳遞到程序裏,方便啓動時需要讀取一些路徑或其它外部參數