C: Parse empty tokens from a string with strtok

后端 未结 8 1117
臣服心动
臣服心动 2020-12-10 19:08

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|         


        
8条回答
  •  萌比男神i
    2020-12-10 19:46

    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.)

提交回复
热议问题