C: Parse empty tokens from a string with strtok

后端 未结 8 1131
臣服心动
臣服心动 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:41

    On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.

    To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token.

    What this say is that it will skip any '|' characters at the beginning of a token. Making 5523 the 5th token, which you already knew. Just thought I would explain why (I had to look it up myself). This also says that you will not get any empty tokens.

    Since your data is setup this way you have a couple of possible solutions:
    1) find all occurrences of || and replace with | | (put a space in there)
    2) do a strstr 5 times and find the beginning of the 5th element.

提交回复
热议问题