Reputation: 33
I am trying to split string ,but unfortunately strtok
behaves weirdly
I have following string get|user=password|23|info|hello
I have tried widely used method using strtok, but unfortunately it treats =
as delimiter and I cannot parse my string.
So get
parsed correctly than parsed only user
, but not user=password
.
Please help to find the problem or suggest any other way to split the string. I am programming for Arduino.
Thanks
Code
const char delimeter = '|';
char *token;
token = strtok(requestString, &delimeter);
// Handle parsed
token = strtok(NULL, &delimeter);
Upvotes: 3
Views: 518
Reputation: 4770
Change this:
const char delimeter = '|';
to this:
const char * delimeter = "|"; // note the double quotes
Upvotes: 2
Reputation: 3977
From cppreference,
delim - pointer to the null-terminated byte string identifying delimiters
The requirement that your approach doesn't fit is null terminated. You take the address of a single char
, but clearly you cannot access anything past this one symbol. strtok
, however, searches for \0
character which terminates the string. Thus you're entering undefined behaviour land.
Instead, use
const char* delimiter = "|";
Upvotes: 4