【问题标题】:Using if/then statements to parse argv[ ] options in C++在 C++ 中使用 if/then 语句解析 argv[ ] 选项
【发布时间】:2012-11-28 17:15:19
【问题描述】:

根据输入的选项/参数,您将如何使用使用 argv[] 的 if/then 语句?

例如,a.out -d 1 sample.txta.out -e 1 sample.txt

int main (int argc, char *argv[])
{
  ifstream infile(argv[3]);

  int c;
  int number = 0;
  int count = 0;

  while ( infile.good() ) {

            if (argv[1] == "-d")
            {

                 c = infile.get();

                 number = atoi(argv[2]);  

                  if (number == count)
                 {
                 cout.put(c);
                 count = 0;
                 }

                      else
                        count++;

            }       


           if (argv[1] == "-e")
           { 
              cout << "entered -e" << endl;   //testing code
           }


  }//end while

}//end main

【问题讨论】:

  • 你的代码有错误吗?
  • 能否请您重述您的问题?你的问题到底是什么?
  • @AshRj - 没有错误,但是当我运行 a.out 时,-d 和 -e 没有任何反应。
  • @harman2012 这是因为程序没有传递给 while() 主体

标签: c++ if-statement argv


【解决方案1】:

你不能使用相等运算符来比较C风格的字符串,你必须使用std::strcmp

if (std::strcmp(argv[1], "-d") == 0)

其背后的原因是== 运算符比较指针而不是它们指向的内容。

【讨论】:

    【解决方案2】:

    希望你要检查输入参数-d-e,对吧? 如果是这种情况,请使用 strcmp()

    如果 (!strcmp(argv[1],"-d")) {

                count++;
                printf("\ncount=%d",count);
    
            }       
    
           if (!strcmp(argv[1],"-e"))
           { 
              printf("entered -e");   //testing code
           }
    

    【讨论】:

      【解决方案3】:

      第一个错误在main的第一行:

      ifstream infile(argv[3]);
      

      你不能这样写,因为没有第三个参数。当你像这样调用你的程序时:

      a.out -d 1 < sample.txt
      

      那么程序看到的命令行是这样的:

      argv[0] = "a.out"
      argv[1] = "-d"
      argv[2] = "1"
      

      相比之下,&lt; sample.txt 由 shell 直接解释,并且文件被流式传输到程序的标准输入 - 您无法在程序内部进行任何更改。

      至于解析本身,不要在内部读取文件的循环中进行,在之前进行并设置适当的标志。

      对于实际的解析,我建议使用库来减轻您的痛苦。标准的 Unix 工具是getopt,但它只有一个 C 接口。有几个 C++ 库,其中 Boost.Program_Options 对我来说有点太复杂了。

      【讨论】:

      • 谢谢大家!我确实必须将其更改为字符串比较,当我更改一些内容时 strcmp 不起作用。
      【解决方案4】:

      argc/argv 来自 C 并且使用起来相当麻烦,所以当完成基本的参数传递时,您可以将参数转换为字符串向量并以 C++ 风格工作:

      #include <iostream>
      #include <vector>
      #include <algorithm>
      #include <iterator>
      
      main(int argc, char* argv[])
      {
        std::vector<std::string> args;
        std::transform(argv+1, argv+argc, std::back_inserter(args), [](char* arg){ return std::string(arg); });
      
        if (args.at(1) == "-d") { std::cout << "Arg 1 = -d" << "\n"; }
      
        for (auto& arg: args) { std::cout << "arg: " << arg << "\n"; }
      }
      

      不过,您需要检查参数是否存在。如果这是一个基本工具,并且当参数不存在时工具中止是可以接受的,那么您可以使用 args.at(x) 而不是 args[x] 访问 args 元素

      或检查this SO question 以获得参数解析器。

      【讨论】:

        【解决方案5】:

        char argv[] 是一个 char * 数组 所以

         if (string(argv[1]) == "-d")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-06-10
          • 2023-03-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-22
          • 1970-01-01
          相关资源
          最近更新 更多