Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Help With strtok

Reply
Thread Tools

Help With strtok

 
 
manochavishal@gmail.com
Guest
Posts: n/a
 
      03-14-2006
Hi
I am writing a Program
in which i get input as

#C1012,S,A#C1013,S,U


I want to get C1012,S,A using strtok and then pass this to function
CreateCopies
which will further strtok this (C1012,S,A) and store the required
values.


Now here is the piece of that code:


#define DELIM2 #
char * field;
char fieldcopy[20];


/*Here i have input as #C1012,S,A#C1013,S,U*/
field = strtok(NULL,DELIM2);
while(field != NULL)
{
strcpy(fieldcopy,field);
CreateCopies(copy,fieldcopy,NoCopies);
field = strtok(NULL,DELIM2);
printf("Field in CreateVideo is %s\n",field);



}


1.Now if I call CreateCopies the strtok doesn't tokenize till the end.
I get to call CreateCopies
only once.
2.But if i comment the CreateCopies call, it does tokenize till the end
and prints the rrquired vales.
In the first case the second time i call strtok 'field' gets a value of
NULL instead it should get the second token.

Why this behaviour???

 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-14-2006
On Tuesday 14 March 2006 09:18, opined (in
< .com>):

> Hi
> I am writing a Program
> in which i get input as
>
> #C1012,S,A#C1013,S,U
>
> I want to get C1012,S,A using strtok and then pass this to function
> CreateCopies which will further strtok this (C1012,S,A) and store the
> required values.
>
> Now here is the piece of that code:
>
> #define DELIM2 #
> char * field;
> char fieldcopy[20];
>
> /*Here i have input as #C1012,S,A#C1013,S,U*/
> field = strtok(NULL,DELIM2);
> while(field != NULL)
> {
> strcpy(fieldcopy,field);
> CreateCopies(copy,fieldcopy,NoCopies);
> field = strtok(NULL,DELIM2);
> printf("Field in CreateVideo is %s\n",field);
> }
>
>
> 1.Now if I call CreateCopies the strtok doesn't tokenize till the end.
> I get to call CreateCopies only once.
> 2.But if i comment the CreateCopies call, it does tokenize till the
> end and prints the rrquired vales.
> In the first case the second time i call strtok 'field' gets a value
> of NULL instead it should get the second token.
>
> Why this behaviour???


You don't show or tell what `CreateCopies` does, but I'll bet you it
uses `strtok` as well. If it does, therein lies your problem. You're
"resetting" `strtok`. You have to finish tokenising the original string
with `strtok(NULL,...)` calls before calling `strtok` again with the
new string to parse.

--
BR, Vladimir

The average, healthy, well-adjusted adult gets up at seven-thirty in
the morning feeling just terrible.
-- Jean Kerr

 
Reply With Quote
 
 
 
 
manochavishal@gmail.com
Guest
Posts: n/a
 
      03-14-2006
>You don't show or tell what `CreateCopies` does, but I'll bet you it
>uses `strtok` as well. If it does, therein lies your problem. You're
>"resetting" `strtok`. You have to finish tokenising the original string
>with `strtok(NULL,...)` calls before calling `strtok` again with the
>new string to parse.


Thanx that did solved my Problem.

I forgot to comment one strtok in CreateCopies.

Thanks again.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with strtok manochavishal@gmail.com C Programming 8 03-16-2006 02:57 PM
strtok ( ) help ern C Programming 13 01-25-2006 01:58 AM
strtok help Glen C++ 1 08-10-2005 07:14 AM
strtok() and std::string Alex Vinokur C++ 6 04-14-2005 01:40 PM
Problems with strtok() returning one too many tokens... Adam Balgach C++ 2 11-28-2004 01:12 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57