Bash Bash - 7 months ago 54
C Question

Parse empty tokens from a string with strtok

My application produces strings like the one below. I need to parse values between the separator into individual values.


I am using
to do this in a loop. For the fifth token, I am getting 5523. However, I need to account for the empty value between the two separators
as well. 5523 should be the sixth token, as per my requirement.

token = (char *)strtok(strAccInfo, "|");

for (iLoop=1;iLoop<=106;iLoop++) {
token = (char *)strtok(NULL, "|");

Any suggestions?


In that case I often prefer a p2 = strchr(p1, '|') loop with a memcpy(s, p1, p2-p1) inside. It's fast, does not destroy the input buffer (so it can be used with const char *) and is really portable (even on embedded).

It's also reentrant; strtok isn't. (BTW: reentrant has nothing to do with multi-threading. strtok breaks already with nested loops. One can use strtok_r but it's not as portable.)