I don't understand the necessity of using strtok() for your problem. Anyway, the answer is just all over the manual for strtok(3):
Quote:
BUGS
Avoid using these functions. If you do use them, note that:
These functions modify their first argument.
These functions cannot be used on constant strings.
The identity of the delimiting character is lost.
The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.
|
To make your code work, you could trick the compiler this way:
Code:
// ...
string str1(" 1 2 3 4 512543 ");
string str2;
if(str2.empty())
{
str2=str1;
str2.insert(0, ""); // now, you tell me why this apparently solves your problem!
}
cout << "str2:" <<str2 <<endl;
p1=strtok((char *)str1.c_str()," ");
// ...