The function strtokuses an internal static buffer to track where it left off. This means that you cannot use it to re-parse two different lines.
In your specific case, with this call:
token = strtok(0, ",");
- current_string, token .
strtok_r. . , :
char *state1, *state2;
char *token = strtok_r(str, ",", &state1);
while(token){
char *current_string = strdup(token);
char *tk = strtok_r(current_string, ":", &state2);
printf("key: %s ", tk);
tk = strtok_r(NULL, ":", &state2);
printf("value: %s\r\n", tk);
printf("%s\n", token);
free(current_string);
token = strtok_r(NULL, ",", &state1);
}
printf("Done\n");
, NULL strtok_r 0, NULL 0. , malloc/strcpy strdup, , free, .
strtok_r UNIX/Linux. Windows strtok_s, .
source
share