Reputation: 97
I have this string that I'm trying to delimit with strtok()
:
"+ 2 3\\nln 9\\ln 10"
And then I'm passing \\n
as a delimiter
token = strtok(argv, "\\n");
while (token)
{
args[index] = token;
token = strtok(NULL, "\\n");
index++;
}
First element in my args
table is + 2 3
,which is great, however, the second one is l
.
Does anyone understand why ? If so, how do I get my ln 9
in my args
table?
Upvotes: 0
Views: 646
Reputation: 1799
I completely agree with above answer and suggestions by Roberto and Gerhardh.
In case if you are fine with custom implementation of strtok for multiple delimeter, you can use below working solution.
char *strtokm(char *str, const char *delim)
{
static char *tok;
static char *next;
char *m;
if (delim == NULL) return NULL;
tok = (str) ? str : next;
if (tok == NULL) return NULL;
m = strstr(tok, delim);
if (m) {
next = m + strlen(delim);
*m = '\0';
} else {
next = NULL;
}
return tok;
}
Upvotes: 1
Reputation: 7490
From strtok() manpage
:
The delim argument specifies a set of bytes that delimit the tokens in the parsed string. The caller may specify different strings in delim in successive calls that parse the same string.
So, in your code, "\\n"
is not a full string delimiter. You are just saying to strtok
that the delimiter is either '\'
(because of the double backspace escaping) or 'n'
.
The tokens of your string, "+ 2 3\\nln 9\\ln 10"
will be:
"+ 2 3"
\
and \
(strtok doesn't present it)\
and n
(strtok doesn't present it)"l"
" 9"
\
and \
(strtok doesn't present it)"l"
" 10"
In order to perform what you are trying to do, strtok
is not the best choice. I would probably write my own parsering function
"\\n"
occurrences in original string using strstr()
Upvotes: 2