![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A program to trace execution of another program | jiten_hegde | High Level Programming | 3 | 08-19-2008 05:26 AM |
| strtok equivalent in perl | jisha | Shell Programming and Scripting | 1 | 05-06-2008 03:38 AM |
| Regardign strtok() output directing to 2-D string array | SankarV | High Level Programming | 3 | 04-28-2008 09:48 AM |
| Strtok function.... | Tanvirk | Linux | 3 | 01-24-2008 10:16 AM |
| better way than strtok? | annie | High Level Programming | 7 | 10-05-2005 02:01 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
How to use strtok twice in the same program?
Code:
string str1(" 1 2 3 4 512543 ");
string str2;
if(str2.empty())
str2=str1;
cout << "str2:" <<str2 <<endl;
p1=strtok((char *)str1.c_str()," ");
while(p1)
{
v1.push_back(atoi(p1));
cout << "val of p1 " << p1 << endl;
p1=strtok(NULL," ");
}
cout << "size of v1 " << v1.size() <<endl;
p2=strtok((char *)str2.c_str()," ");
cout << "str2:" <<str2 <<endl;
while(p2)
{
v2.push_back(atoi(p2));
cout << "val of p2 " << p2 <<endl;
p2=strtok(NULL," ");
}
cout << "size of v2" << v2.size() <<endl;
Code:
str2: 1 2 3 4 512543 val of p1 1 val of p1 2 val of p1 3 val of p1 4 val of p1 512543 size of v1 5 str2: 1234512543 val of p2 1 after tok p2 size of v21 How can I tokenize str2 now? Thanks |
|
||||
|
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:
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()," ");
// ...
|
|
||||
|
better use strtok_r
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|