strtok - How to split nested deliminated strings in c? -
i wondering, how able split kind of strings.
for example have following string: "80,8080,27001-27010,90"
i first want split @ comma if there minus in substring want split , difference between 2 numbers converting them via atoi(). string want split list of ports, , want generate array of integer ports enumerated. strtok-function seems not suitable problem, there easy solution this?
using of strtok_r example:
substr1 = strtok_r(portlist, delim1, &saveptr1); while(substr1 != null){ substr2 = strtok_r(substr1, delim2, &saveptr2); substr1 = strtok_r(null, delim2, &saveptr1); }
in particular case, can use strchr
find point split token, thereby avoiding losing progress.
another way use strtok_r
, reentrant version of strtok
doesn't have internal state.
edit:
strtok_r
not part of standard c. part of posix.1-2001. think major compilers support it. in windows, corresponding function called strtok_s
. way make portable
#if defined(_win32) || defined(_win64) /* on windows */ # define strtok_r strtok_s #endif
as described in joachim pileborgs answer what's difference between strtok_r , strtok_s in c?
Comments
Post a Comment