【问题标题】:Using getopt to process simple command line arguments [duplicate]使用 getopt 处理简单的命令行参数 [重复]
【发布时间】:2018-05-24 06:58:24
【问题描述】:

我想处理一些命令行选项。

int main(int argc, char **argv) {
  char c;
  int n = 10000;
  int nThreads = 3;

  while ((c = getopt (argc, argv, "n:t:")) != -1){
    switch (c)
      {
      case 'n':
        n = *optarg;
        break;
      case 't':
        nThreads = *optarg;
        break;
      default:
        abort();
      }
  }

  printf("n=%d, nThreads=%d\n", n, nThreads);
}

但这并不像我预期的那样工作。运行./program -n 10000 -t 2会导致程序输出n=49, nThreads=50

我错过了什么?

【问题讨论】:

    标签: c command-line-arguments getopt


    【解决方案1】:
    n = atoi(optarg);
    

    否则,您会将char 分配给int

    您还可以看到4950 的来源。

    字符1的Ascii值为49250

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 1970-01-01
      相关资源
      最近更新 更多