我已經從文件中取出所有的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