My application produces strings like the one below. I need to parse values between the separator into individual values.
2342|2sd45|dswer|2342||5523|||3654|Pswt
I am using strtok 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 amemcpy(s, p1, p2-p1)inside. It’s fast, does not destroy the input buffer (so it can be used withconst char *) and is really portable (even on embedded).It’s also reentrant;
strtokisn’t. (BTW: reentrant has nothing to do with multi-threading.strtokbreaks already with nested loops. One can usestrtok_rbut it’s not as portable.)