The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 10-04-2008
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
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()," ");
// ...