strtok

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • New

    strtok

    I have a String from a file in the form <number> <word>,<word>,< word>,<word>
    <number>
    when I try to tokenize it I store it in a char[] and tokenize it using
    strtok(string, " ") and then tokenize the orignal string using
    strtok(string," ,")

    the first tokenization gives me only the first number and then NULL's
    and the second tokenization gives me 1st char[]=<word> 2nd char[]=<word> 3rd
    char[]=<word>
    4th char[]=<word> <number>

    What am I doing wrong?


  • Dan Pop

    #2
    Re: strtok

    In <412dae53_1@new s.iprimus.com.a u> "New" <a@a.co> writes:
    [color=blue]
    >I have a String from a file in the form <number> <word>,<word>,< word>,<word>
    ><number>
    >when I try to tokenize it I store it in a char[] and tokenize it using
    >strtok(strin g, " ") and then tokenize the orignal string using
    >strtok(string, ",")
    >
    >the first tokenization gives me only the first number and then NULL's
    >and the second tokenization gives me 1st char[]=<word> 2nd char[]=<word> 3rd
    >char[]=<word>
    >4th char[]=<word> <number>
    >
    >What am I doing wrong?[/color]

    The proper sequence of strtok calls for your purpose is:

    strtok(string, " ");
    strtok(NULL, ",");
    strtok(NULL, ",");
    strtok(NULL, ",");
    strtok(NULL, " ");
    strtok(NULL, "");

    Consider tokenising the whole string with a single sscanf call.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email: Dan.Pop@ifh.de

    Comment

    • bowsayge

      #3
      Re: strtok

      New said to us:
      [color=blue]
      > I have a String from a file in the form <number> <word>,<word>,< word>,<word>
      > <number>
      > when I try to tokenize it I[/color]
      [...]

      You need to change the delimiters string while you loop over the
      tokens:

      char str [] = "4116 black,10 feet,stationary ,none 18";
      char * delim = " ";
      char * ptr1 = 0;
      char * ptr2 = 0;
      int field = 0;
      ptr1 = str;
      while ((ptr2 = strtok(ptr1, delim))) {
      ptr1 = 0;
      delim = ",";
      if (++field == 4) { delim = " "; }
      printf ("token: %s\n", ptr2);
      }

      Comment

      • Tim Hagan

        #4
        Re: strtok

        "bowsayge" <bowsayge@nomai l.afraid.org> wrote in message
        news:ZelXc.1327 9$2L3.1119@news read3.news.atl. earthlink.net.. .[color=blue]
        > New said to us:
        >[color=green]
        > > I have a String from a file in the form <number> <word>,<word>,< word>,<word>
        > > <number>
        > > when I try to tokenize it I[/color]
        > [...]
        >
        > You need to change the delimiters string while you loop over the
        > tokens:
        >
        > char str [] = "4116 black,10 feet,stationary ,none 18";
        > char * delim = " ";
        > char * ptr1 = 0;
        > char * ptr2 = 0;
        > int field = 0;
        > ptr1 = str;
        > while ((ptr2 = strtok(ptr1, delim))) {
        > ptr1 = 0;
        > delim = ",";
        > if (++field == 4) { delim = " "; }
        > printf ("token: %s\n", ptr2);
        > }
        >[/color]

        If <word> is a single word, then this will work:

        #include <stdio.h>
        #include <string.h>

        int main(void)
        {
        char s[] = "123 abc,def,ghi,jkl 456";
        char *d = " ,";
        char *p = s;
        char *t;

        printf("string: %s\n", s);
        while (t = strtok(p, d))
        {
        printf("token: %s\n", t);
        p = NULL;
        }
        return 0;
        }

        --
        Tim Hagan



        Comment

        Working...