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