strtok_s behaviour with consecutive delimiters

前端 未结 2 1451
死守一世寂寞
死守一世寂寞 2021-01-14 19:55

I\'m parsing 3 values in parallel which are separated with a specific separator.

token1 = strtok_s(str1, separator, &nextToken1);
token2 = strtok_s(str2,         


        
相关标签:
2条回答
  • 2021-01-14 20:01

    Unfortunately, strtok() ignores empty tokens. Even though you said you wish to avoid doing that, there is no other way but to parse it yourself, using for example strchr() to find the next delimiter and then copying the token to a temporary variable for processing. This way you can handle empty tokens whichever way you please.

    0 讨论(0)
  • 2021-01-14 20:18

    Yes, that's the way this function works. It's more appropriate for tasks like parsing words where multiple whitespace characters should not be treated as empty words.

    I've done a lot of parsing. I would simply write my own parser here, where the code examines one character at a time. It's not that difficult and you can make it behave exactly how you need. As an example, I've posted some C++ code to parse a CSV file in my article Reading and Writing CSV Files in MFC

    0 讨论(0)
提交回复
热议问题