strtok() is a poorly designed function to get you started. Check your documentation to see if you have the best. BTW, never use strtok() in any kind of streaming environment unless your docs talk about it safely, as it stores state between calls and changes the line it called. I assume strtok_s() is a more secure version, but it will not be really safe.
To convert a std::string to char * , you can do:
char * temp_line = new char[line.size() + 1];
and use temp_line . Your installation may have a strdup() function that will duplicate the above.
The reason you need two calls to strtok_s() is because they do different things. The first tells strtok_s() which line it should work on, and the second with the same line. This is the reason for the NULL argument; it tells strtok_s() to continue working with the source string.
Therefore, you need one call to get the first token, and then one for each subsequent token. They could be combined with something like
char * temp_string_pointer = temp_line; while ((token = strtok_s( con, "#", &next_token)) != NULL) { temp_string_pointer = NULL;
etc., since it will call strtok_s() once with a pointer to a string and after that with NULL . Do not use temp_line for this, since you want delete[] temp_line; after processing.
You might think that this is a lot of messing around, but what strtok() and relatives usually mean.
source share