我正在尝试读取如下所示的命令行参数
./program -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 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];
}
最佳答案
与其打开第二个字符(仅适用于单个字母),不如尝试使用返回 0(等于)、正数的 strcmp(const char *lhs, const char *rhs)
(左旋在右旋之后),还是负数(左旋在右旋之前)?
例如:
#include <string.h>
// ....
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-v") == 0) {
// ...
}
else if (strcmp(argv[i], "-aB") == 0) {
// ...
}
// ...
}
关于c - 读取命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55409112/