c - 读取命令行参数

标签 c

我正在尝试读取如下所示的命令行参数

./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/

相关文章:

c - C中的字符串链表

c - 如何从函数返回地址并在 main 中收集?

c++ - LPCSTR 没有 'long' 并且 UINT_PTR 没有指针?

c - for 循环 C 中的 OR 运算符

c - 如何编写根据命令行参数将字母转换为大写或小写的程序?

c - C : Execvp not finishing execution when wait is added to parent 中的 Linux Shell

c++ - 传递 malloc 指针给我错误

C中的循环链表

objective-c - 混淆 LLDB 输出

c - 我应该在 Visual Studio 中投 void** 返回吗