Help With strtok

Discussion in 'C Programming' started by manochavishal@gmail.com, Mar 14, 2006.

  1. Guest

    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???
     
    , Mar 14, 2006
    #1
    1. Advertising

  2. On Tuesday 14 March 2006 09:18, opined (in
    <>):

    > 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
     
    Vladimir S. Oka, Mar 14, 2006
    #2
    1. Advertising

  3. Guest

    >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.
     
    , Mar 14, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam Balgach
    Replies:
    2
    Views:
    599
    news-east
    Nov 28, 2004
  2. Alex Vinokur

    strtok() and std::string

    Alex Vinokur, Apr 14, 2005, in forum: C++
    Replies:
    6
    Views:
    4,970
    Pete Becker
    Apr 14, 2005
  3. ern

    strtok ( ) help

    ern, Jan 20, 2006, in forum: C Programming
    Replies:
    13
    Views:
    850
  4. Help with strtok

    , Mar 14, 2006, in forum: C Programming
    Replies:
    8
    Views:
    389
    CBFalconer
    Mar 16, 2006
  5. Glen

    strtok help

    Glen, Aug 10, 2005, in forum: C++
    Replies:
    1
    Views:
    400
Loading...

Share This Page