我有下一個代碼:命令行分析器++
#include "CmdLine.h"
void main(int argc, TCHAR **argv)
{
CCmdLine cmdLine;
// parse argc,argv
if (cmdLine.SplitLine(argc, argv) < 1)
{
// no switches were given on the command line, abort
ASSERT(0);
exit(-1);
}
// test for the 'help' case
if (cmdLine.HasSwitch("-h"))
{
show_help();
exit(0);
}
// get the required arguments
StringType p1_1, p1_2, p2_1;
try
{
// if any of these fail, we'll end up in the catch() block
p1_1 = cmdLine.GetArgument("-p1", 0);
p1_2 = cmdLine.GetArgument("-p1", 1);
p2_1 = cmdLine.GetArgument("-p2", 0);
}
catch (...)
{
// one of the required arguments was missing, abort
ASSERT(0);
exit(-1);
}
// get the optional parameters
// convert to an int, default to '100'
int iOpt1Val = atoi(cmdLine.GetSafeArgument("-opt1", 0, 100));
// since opt2 has no arguments, just test for the presence of
// the '-opt2' switch
bool bOptVal2 = cmdLine.HasSwitch("-opt2");
.... and so on....
}
我已經實現了CCmdLine類和該主是如何使用它的一個爲例。 我很難理解我如何獲得輸入值。我試圖用控制檯上的scanf讀取它們,但是argc不會增加並導致讀取錯誤。
我是一個初學者在c + +,我想知道是誰使這個代碼工作。
謝謝。
嘿嘿,謝謝。這是有用的,我瞭解argc和argv的工作,但你是什麼處理輸入自己的意思嗎?你能給我一個例子嗎? 。謝謝。 –
@Ion,我的意思是你需要保存你從控制檯中得到的東西:'void main(void){char string [128]; cin.getline(string,sizeof(string));/*從控制檯獲取輸入,並將其放入字符串變量* /}' – SingerOfTheFall
我現在明白了。謝謝你的幫助。 –