The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-04-2008
sathishkmrv sathishkmrv is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 6
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;
I get the following o/p.

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
I want str2 also to be tokenized. But strtok after reaching NULL once it returns only NULL pointer everytime when it is accessed using NULL.

How can I tokenize str2 now?

Thanks
  #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()," ");
// ...
  #3 (permalink)  
Old 10-04-2008
sathishkmrv sathishkmrv is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 6
How to use strtok twice in the same program?

Yes. It worked!!!
But how did the insertion of
Code:
str2.insert(0, "");
solved the problem?
The manpages also suggests that it is not advisable to use.
  #4 (permalink)  
Old 10-07-2008
Saurabh78 Saurabh78 is offline
Registered User
  
 

Join Date: Jun 2004
Posts: 38
better use strtok_r
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 04:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0