Reputation: 11
I am using strtok_s to pull out a value from a C-String based on user input. However, I need to be able to call this function more than one time. Currently when I do it has a problem going through strtok_s. I believe because it is still pointing to a different location and not starting again. ListOfCoeffs is my c-string, which is just a c-string list that has doubles. Degree is the int value that is passed into the function from the user. Is there any way to "reset" strtok so that it will allow me to use this function more than one time without shutting the program down? Apologies for poor style using strtok_s, I am not familiar with it at all.
char * pch;
double coeffValue;
char * context;
pch = strtok_s(listOfCoeffs, " ", &context);
if (degree == 0)
{
// DO NOTHING
}
else
{
for (int i = 0; i < degree; i++)
{
pch = strtok_s(NULL, " ", &context);
}
}
coeffValue = atof(pch);
return coeffValue;
Upvotes: 0
Views: 836
Reputation: 46923
The strtok
family of functions destructively modify the input string while tokenizing it. If you want to potentially start over at the beginning, you need to copy the string before passing it to strtok
.
Upvotes: 3