我已经从文件中取出所有的txt并逐行放入字符串数组中。我想分割这个字符串,以便我可以单独保存在单独的数组中。请告诉我shell是如何将字符串数组转换为char的。将字符串转换为字符
例如
string line[15]; // line[0] has : was there before
// line[1] has : then after
char * pch;
char *c = line.c_str(); // string to char (i am getting error here. Any body know?)
pch = strtok (c," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ");
}
错误:C2228:左 '.c_str' 必须具有类/结构/联合
'line'是不是一个字符串。它是一串字符串。你也许可以做'line [0] .c_str()'。你还需要使用'const char * pch;'或者你会得到另一个错误。 – 2014-11-06 18:33:20
'line'是一个数组。它不是'class/struct/union'。你的意思是(例如)'char * c = line [0] .c_str();'?此外,您可以编写自己的使用'std :: string'的令牌函数,而不是使用旧的c函数。 – clcto 2014-11-06 18:33:59
为了加入我最后的评论,我写了一个'tokenize'函数,可以在https://github.com/clcto/utile/blob/master/src/utile.cpp行235 – clcto 2014-11-06 18:39:30