读取命令行参数

时间:2019-03-29 01:06:26

标签: 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 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];

    }

1 个答案:

答案 0 :(得分:1)

与其打开第二个字符(仅适用于单个字母),不如尝试使用strcmp(const char *lhs, const char *rhs)来返回0(等于),正数(在rhs之后的lhs)或负数(在rhs之前的lhs)?

例如:

#include <string.h>
// ....
for (int i = 1; i < argc; ++i) {
  if (strcmp(argv[i], "-v") == 0) {
    // ...
  }
  else if (strcmp(argv[i], "-aB") == 0) {
    // ...
  }
  // ...
}
相关问题