问题描述
我已经改变了我的code,趁现在发生这些错误编译:
`check.cpp:在函数'廉政的main()':`
check.cpp:14:55:错误:从无效转换'为const char **'到'char * const的*'[-fpermissive]
/usr/include/getopt.h:152:12:错误:初始化的'诠释的getopt(INT,char * const的*,为const char *)[-fpermissive] 参数2 code>
INT的main(){字符串文本= - ˚Finput.gmn -output.jpg
INT ARGC = text.length();
COUT<<ARGC:<<&ARGC LT;< ENDL;
字符常量* argv的= text.c_str();
COUT<<argv的:<<&argv的LT;< ENDL;
INT C = getopt的(ARGC,&安培; argv的,F:S:○:PW:H:Z:T:D:A:B:?);
返回0;
}
您可以使用 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的一个好处是,你可以使用其他字符作为分隔符,而不仅仅是空间,而且你不必在意,因为<$ C $释放分配的内存C>的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.
这篇关于如何字符串转换成char *数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!