从std :: str转换为char [英] Converting from std::str to char
问题描述
你好,
我?以下代码行有问题:
字符串行;
ifstream myfile(" doorcoordinates.txt");
if(myfile.is_open())
{
char str [100 ];
char * pch;
getline(myfile,line);
str = line;
pch = strtok(str,";");
while(pch!= NULL)
{
pch = strtok(NULL, ",。");
}
myfile.close();
}
line:
str = line;
是问题,因为编译器说,他无法转换为
std :: str to char [100]。
如何从std :: str转换/转换为char [100]?
非常感谢你的帮助,
马丁
Hello,
I?′ve a problem with the follwing code lines:
string line;
ifstream myfile ("doorcoordinates.txt");
if (myfile.is_open())
{
char str[100];
char * pch;
getline (myfile,line);
str=line;
pch = strtok (str,";");
while (pch != NULL)
{
pch = strtok (NULL, " ,.");
}
myfile.close();
}
line:
str=line;
is the problem, because the compiler says, that he cannot convert from
std::str to char[100].
How do I convert/cast from std::str to char[100]?
Thanks a lot for help,
Martin
推荐答案
Martin P ?? pping写道:
Martin P??pping wrote:
你好,
我对以下代码行有问题:
字符串行;
ifstream myfile(" doorcoordinates.txt");
if(myfile.is_open())
{
char str [100];
char * pch;
getline(myfile,line);
str = line;
pch = strtok(str," ;;");
while(pch!= NULL)
{
pch = strtok(NULL," ,。);
}
myfile.close();
}
行:
str = line;
是问题,因为编译器说,他不能从
std :: str转换为char [100]。
如何从std :: str转换/转换为char [100]?
Hello,
I?′ve a problem with the follwing code lines:
string line;
ifstream myfile ("doorcoordinates.txt");
if (myfile.is_open())
{
char str[100];
char * pch;
getline (myfile,line);
str=line;
pch = strtok (str,";");
while (pch != NULL)
{
pch = strtok (NULL, " ,.");
}
myfile.close();
}
line:
str=line;
is the problem, because the compiler says, that he cannot convert from
std::str to char[100].
How do I convert/cast from std::str to char[100]?
strtok接受一个const char *,所以不要设置所有临时的
变量,只是做这个:
pch = strtok(line.c_str()," ;;");
Ben Pope
-
我不仅仅是一个数字。对很多人来说,我被称为字符串...
strtok takes a const char *, so don''t set up all the temporary
variables, just do this:
pch = strtok (line.c_str(),";");
Ben Pope
--
I''m not just a number. To many, I''m known as a string...
> ...
line:
str =线路;
...
是问题,因为编译器说,他不能从
std :: str转换为char [100]。
line:
str=line;
...
is the problem, because the compiler says, that he cannot convert from
std::str to char[100].
sprintf(str,"%s",line.c_str());
也许这对于普通来说有点不对。 C ++,
但是有效。
sprintf(str, "%s", line.c_str());
Maybe it''s little wrong for "plain" C++,
but it works.
Ben Pope写道:
Ben Pope wrote:
strtok采用const char *,
不,它没有。
所以不要设置所有临时的变量,只需这样做:
pch = strtok(line.c_str()," ;;");
strtok takes a const char *,
No, it doesn''t.
so don''t set up all the temporary
variables, just do this:
pch = strtok (line.c_str(),";");
这不应该编译。
This shouldn''t compile.
这篇关于从std :: str转换为char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!