Search code examples
cstrtok

Can a delim be used more than once for strtok in C?


Is there a way to use a delim more then once for strtok? I can only get the code to work for either load or init but not both. Example code in which strtok is used to token input from the user in the form of load # or init #,#,direction:

if ((tok = strtok(choice, " ")) && strcmp(tok, COMMAND_LOAD) == 0){
    tok = NULL;
    if((tok = strtok(tok, " ")) != NULL){
    /*some code*/
    }
}
else if((tok = strtok(choice, " ")) && strcmp(tok, COMMAND_INIT) == 0){
    tok = NULL;
    if((tok = strtok(tok, ",")) != NULL){
    /*some code*/
    }
}

Solution

  • The strtok function is not reentrant, you can not use it for multiple simultaneously tokenizations.

    Use strtok_s instead, if you have it. Or strtok_r is you have it.