C 读取命令行参数
我试图读取一个如下所示的命令行参数 /程序-aB-v 但我似乎无法理解如何读取-aB命令 我试着把aB放在我的开关上,但没用。 这就是我所使用的代码C 读取命令行参数,c,C,我试图读取一个如下所示的命令行参数 /程序-aB-v 但我似乎无法理解如何读取-aB命令 我试着把aB放在我的开关上,但没用。 这就是我所使用的代码 void processCommandSwitches(int argc, char *argv[], char **ppszFileWidgets, Simulation sim){ int i; // Examine each of the command arguments other than the name of the
void processCommandSwitches(int argc, char *argv[], char **ppszFileWidgets, Simulation sim){
int i;
// Examine each of the command arguments other than the name of the program.
for (i = 1; i < argc; i++)
{
switch (argv[i][1])
{
case 'v':
sim->bVerbose = TRUE;
break;
case '?':
*ppszFileWidgets = argv[i];
break;
default:
*ppszFileWidgets = argv[i];
}
*ppszFileWidgets = argv[i];
}
void processCommandSwitches(int-argc,char*argv[],char**ppszFileWidgets,Simulation-sim){
int i;
//检查除程序名称以外的每个命令参数。
对于(i=1;ibVerbose=TRUE;
打破
案例“?”:
*ppszFileWidgets=argv[i];
打破
违约:
*ppszFileWidgets=argv[i];
}
*ppszFileWidgets=argv[i];
}
与其打开第二个字符(仅适用于单个字母),不如尝试使用strcmp(const char*lhs,const char*rhs)返回0(相等)、正(lhs在rhs之后)或负(lhs在rhs之前)
例如:
#包括
// ....
对于(int i=1;i
#include <string.h>
// ....
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-v") == 0) {
// ...
}
else if (strcmp(argv[i], "-aB") == 0) {
// ...
}
// ...
}