简体   繁体   English

如何将字符串转换为char * agv []

[英]How to convert string into char *agv[]

I am building a command line tool and at the beginning whole line is a string. 我正在构建一个命令行工具,并且在开始时整行是一个字符串。 How could I convert: 我该如何转换:

  string text = "-f input.gmn -output.jpg";

into 进入

  const char *argv[] = { "ProgramNameHere", "-f", "input.gmn", "-output.jpg" };

If I had to use getopt , and I knew I was starting with a white-space separated std::string , I'd do this: 如果我必须使用getopt ,并且我知道我是以空格分隔的std::string开头的,那么我会这样做:

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <cassert>
#include <cstring>

int main() {

    https://stackoverflow.com/questions/236129/how-to-split-a-string-in-c

    // My input
    std::string sentence = "-f input.gmn -output.jpg";

    // My input as a stream
    std::istringstream iss(sentence);

    // Create first entry
    std::vector<std::string> tokens;
    tokens.push_back("ProgramNameHere");

    // Split my input and put the result in the rest of the vector
    std::copy(std::istream_iterator<std::string>(iss),
        std::istream_iterator<std::string>(),
        std::back_inserter(tokens));

    // Now we have vector<string>, but we need array of char*. Convert to char*
    std::vector<char *> ptokens;
    for(auto& s : tokens)
        ptokens.push_back(&s[0]);

    // Now we have vector<char*>, but we need array of char*. Grab array
    char **argv = &ptokens[0];
    int argc = ptokens.size();

    // Use argc and argv as desired. Note that they will become invalid when
    // *either* of the previous vectors goes out of scope.
    assert(strcmp(argv[2], "input.gmn") == 0);
    assert(argc == 4);

}

See also: Split a string in C++? 另请参阅: 在C ++中拆分字符串?


Postscript : In the solution I provided, I used two language features introduced in C++2011: range-based for loops and type inference . 后记 :在我提供的解决方案中,我使用了C ++ 2011中引入的两种语言功能: 基于范围的循环类型推断

This code fragment will only compile if your compiler supports thew new features: 仅当您的编译器支持以下新功能时,此代码片段才会编译:

  for(auto& s : tokens) ptokens.push_back(&s[0]); 

If you have an older C++ compiler, you might need to rewrite it using C++2003 features: 如果您使用的是较旧的C ++编译器,则可能需要使用C ++ 2003功能对其进行重写:

  for(std::vector<string>::iterator it = tokens.begin(); it != tokens.end(); ++it) ptokens.push_back(it->c_str()); 

or 要么

  for(std::vector<string>::size_type i = 0; i < tokens.size(); ++i) ptokens.push_back(tokens[i].c_str()); 

I would recommend using boost::program_options to parse your program's arguments. 我建议使用boost :: program_options解析程序的参数。

Otherwise if you are using MSVC, you might want to use the built-in __argc and __argv. 否则,如果您使用的是MSVC,则可能要使用内置的__argc和__argv。

There is no portable way to get your program's image name, so you cannot get that information out of nowhere if you dropped it in the first place by discarding your original argv argument. 没有可移植的方法来获取程序的映像名称,因此,如果首先通过丢弃原始的argv参数将其删除,就无法从一无所获。

You could use the C strtok function to split your arguments ... actually scratch that, just use boost::algorithm::split with any_of(' '). 您可以使用C strtok函数拆分参数 ...实际上是为了解决问题,只需使用boost :: algorithm :: split与any_of('')。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM