我需要将一个字符串转换为char *以便在strtok_s中使用,但一直无法弄清楚。c_str()将其转换为const char *,不兼容。
此外,如果有人能解释一下为什么需要第二个strtok_s函数(循环内部),那将是非常有帮助的。为什么我需要显式地推进标记,而不是例如它所在的while循环,连续获取文件的每一行。
while( getline(myFile, line) ) { // Only one line anyway. . . is there a better way?
char * con = line.c_str();
token = strtok_s( con, "#", &next_token);
while ((token != NULL))
{
printf( " %s\n", token );
token = strtok_s( NULL, "#", &next_token);
}
}
related question.
strtok
实际上不起作用,因为它们可能会错误地检测到字符串的结尾。 - Steve Jessop