I am currently having some strange results when using strsep
with multiple delimiters. My delimiters include the TAB character, the space character, as well as >
and <
.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
char buffer[50];
char *curr_str = NULL;
const char delim[4] = "\t >";
//const char delim[4] = "\t ><"; // This does not work
snprintf(buffer, 50, "%s", "echo Hello");
char *str_ptr = buffer;
curr_str = strsep(&str_ptr, delim);
if (curr_str != NULL)
printf("%s\n", curr_str);
curr_str = strsep(&str_ptr, delim);
if (curr_str != NULL)
printf("%s\n", curr_str);
return (0);
}
This output is what I expect.
echo
Hello
However, as soon as I add the '<' character for the delimiter, I get
cho
Somehow, the first character gets cut off. Is there a reason behind why this is occurring?
Thank you.
The second argument to
strsep
,delim
is a null-terminated string (like all strings in C), so you have to leave space for the terminating character:If you don't end the string, it will go exploring memory past the array and find many new delimiting characters to use, which is what happened in your case.