Help With strtok

M

manochavishal

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???
 
V

Vladimir S. Oka

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
 
M

manochavishal

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help with strtok 8
Access violation reading location 0
strtok 7
Why does strcat mess up the tokens in strtok (and strtok_r)? 92
strtok problem 16
strtok() 13
Can't solve problems! please Help 0
problems with strtok() 2

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top