It is important to realize that strtok()
modifies the source string in-place, and returns pointers into it.
Thus, the two calls to strtok()
turn str
into
#include \0hello.txt\0
^ subString points here
(For simplicity, I don't show the final terminating \0
).
Now, the second ("problematic") strcpy()
changes str
back to:
#include "hello.txt"
^ subString still points here
This is what makes the "
reappear in subString
.
One way to fix it is by tokenizing a copy and keeping the original intact. Just make sure that your function doesn't return a pointer to an automatic variable (that would go out of scope the moment the function returns).