Convert command line argument to string

后端 未结 7 1261
囚心锁ツ
囚心锁ツ 2021-01-31 08:43

I have a program that reads hard-coded file-path and I want to make it read file-path from command line instead. For that purpose I changed the code like this:

#         


        
7条回答
  •  不要未来只要你来
    2021-01-31 09:15

    You can create an std::string

    #include 
    #include 
    int main(int argc, char *argv[])
    {
      // check if there is more than one argument and use the second one
      //  (the first argument is the executable)
      if (argc > 1)
      {
        std::string arg1(argv[1]);
        // do stuff with arg1
      }
    
      // Or, copy all arguments into a container of strings
      std::vector allArgs(argv, argv + argc);
    }
    

提交回复
热议问题