strtokis a pretty dodgy, evil function that modifies its argument. This means that you cannot use it directly in the content std::string, since there is no way to get a pointer to a mutable array of characters with a null character from this class.
You can work with a copy of string data:
std::vector<char> buffer(line.c_str(), line.c_str()+line.size()+1);
char * pch = strtok(&buffer[0], ",");
or, for the larger C ++ idiom, you can use a stream of strings:
std::stringstream ss(line);
std::string token;
std::readline(ss, token, ',');
or find a comma more directly:
std::string token(line, 0, line.find(','));
source
share