以前C讀取命令行參數(shù)感覺太麻煩,而且不容易記憶.下面是另外一種命令行的讀取方法
通過GetCommandLine(),來返回當(dāng)前的命令行:
setlocale(LC_ALL, ".ACP");
std::wcout << GetCommandLine() << std::endl;
下面是輸出:
"e:\學(xué)習(xí)程序\consol3\debug\consol3.exe" a b c
對于命令行的參數(shù)讀取用CommandLineToArgvW,可以把命令行參數(shù)轉(zhuǎn)換成字符串?dāng)?shù)組的形式
int?nums = 0;
TCHAR** temp = CommandLineToArgvW(GetCommandLine(),&nums);
for(int i = 0; i < nums; i++)
{
? std::wcout<< temp[i] << std::endl;
}
下面是輸出:
e:\學(xué)習(xí)程序\consol3\debug\consol3.exe
a
b
c
可以看到命令行參數(shù)從下標(biāo)=1的元素開始.
感覺這種形式比較好記憶