且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何字符串转换成char *数组

更新时间:2023-02-03 08:46:50

您可以使用 text.c_str()来转换的std ::字符串为const char * 。请参见这里

要阐述我的答案,有很多方法可以创建你所需要的阵列,但这已经是这里描述 一>和这里。一个简单的解决您的问题,不涉及 / 的malloc 或STL的集约利用和 istringstream / back_inserter / 复制不和执行非常快可能是什么样子这个

  / *变量。 * /
的std ::矢量<字符*> argv的;
INT I,ARGC,状态;
焦炭℃;/ *字符串转换为char字符串自动垃圾收集。 * /
的std ::矢量<焦炭>令牌(text.begin(),text.end());
tokens.push_back(0);/ *记号化字符串空间。 * /
为(状态= 0,的argc = 0,I = 0;(C =令牌由[i]);我++){
    如果(州){
        如果(C ==''){
            令牌由[i] = 0;
            状态= 0;
        }
    }其他{
        如果(C!=''){
            argv.push_back(安培;令牌[I]);
            ARGC ++;
            状态= 1;
        }
    }
}/ * argv的打印。 * /
性病::法院LT&;< ARGC:<< ARGC<<的std :: ENDL;
对于(i = 0; I< ARGC,我++){
    性病::法院LT&;< 的argv [<< I<< ]:<<的argv [1] - ;<的std :: ENDL;
}/ *调用getopt的。 * /
C = getopt的(ARGC,和放大器;的argv [0],F:S:○:PW:H:Z:T:D:A:B:?);

这只是一个例子,但这种code的一个好处是,你可以使用其他字符作为分隔符,而不仅仅是空间,而且你不必在意,因为的std ::矢量这是否对你在函数出口。

I have changed my code, now while the compilation these errors occur:

`check.cpp: In function ‘int main()’:`

check.cpp:14:55: error: invalid conversion from ‘const char**’ to ‘char* const*’ [-fpermissive]

/usr/include/getopt.h:152:12: error: initializing argument 2 of ‘int getopt(int, char* const*, const char*)’ [-fpermissive]

int main() {

string text="-f  input.gmn -output.jpg";
int argc=text.length();
cout<<"argc: "<<argc<<endl;
char const * argv = text.c_str();
cout<<"argv: "<<argv<<endl;
int c = getopt (argc, &argv, "f:s:o:pw:h:z:t:d:a:b:?");
return 0;
}

You can use text.c_str() to convert a std::string into a const char*. See here.

To elaborate on my answer, there are many ways to create the array you need, but this is already described here, here, here and here. A simple solution to your problem that does not involve new/malloc or intensive uses of the STL and istringstream/back_inserter/copy what not and performs really fast could look like this:

/* variables. */
std::vector< char* > argv;
int i, argc, state;
char c;

/* convert string to char string with automatic garbage collection. */
std::vector< char > tokens(text.begin(), text.end());
tokens.push_back(0);

/* tokenize string with space. */
for (state=0, argc=0, i=0; (c=tokens[i]); i++) {
    if (state) {
        if (c == ' ') {
            tokens[i]=0;    
            state=0;        
        }           
    } else {
        if (c != ' ') {
            argv.push_back(&tokens[i]);
            argc++;         
            state=1;        
        }           
    }       
}   

/* print argv. */
std::cout << "argc: " << argc << std::endl;
for (i=0; i < argc; i++) {
    std::cout << "argv[" << i << "]: " << argv[i] << std::endl;
}   

/* call getopt. */
c = getopt(argc, &argv[0], "f:s:o:pw:h:z:t:d:a:b:?");

This is just an example, but one advantage of this kind of code is that you can use other characters as delimiter, not just space, and that you need not care about releasing the allocated memory since std::vector does this for you on function exit.