tokens

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deika
    New Member
    • Sep 2007
    • 1

    #1

    tokens

    what are tokens and what does strtok do?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by deika
    what are tokens and what does strtok do?
    I'll move your question over to the C/C++ Forum section where it belongs.

    kind regards,

    Jos

    Comment

    • Studlyami
      Recognized Expert Contributor
      • Sep 2007
      • 464

      #3
      strtok tells how you want to separate a string and where you want to store it.
      Heres a basic example.
      Code:
          FILE *f_ptr;  //points to an open text file
          f_ptr = fopen("MyText.txt", "rt"); //opens the text file
      
          if(f_ptr == NULL) //error opening file
              return;
      
          char * c_Ptr = NULL; // holds a temparay selection sections
          char c_Ptr1[1000] = {NULL}; //holds the entire line of text
      
          for(;fgets(c_Ptr1, 1000, f_ptr)!=NULL;) //grabs the next line of text
          {
              c_Ptr = strtok(c_Ptr1, ","); // first time to separate hold the location in the line
              while (c_Ptr != NULL)
              c_Ptr = strtok(NULL, ","); 
      }
      if the first line of text said "this is, the line of, text separated by a specific token"

      the first time strtok c_Ptr = this is
      2nd strtok c_Ptr = the line of
      3rd strtok c_Ptr = text separated by a specific token

      Then when it hits the for loop it grabs the next line in the text file until it find the end of file.

      Comment

      • Studlyami
        Recognized Expert Contributor
        • Sep 2007
        • 464

        #4
        Wow my example is pretty ugly here is a much cleaner example fro www.cplusplus.c om

        Code:
        /* strtok example */
        #include <stdio.h>
        #include <string.h>
        
        int main ()
        {
          char str[] ="- This, a sample string.";
          char * pch;
          printf ("Splitting string \"%s\" into tokens:\n",str);
          pch = strtok (str," ,.-");
          while (pch != NULL)
          {
            printf ("%s\n",pch);
            pch = strtok (NULL, " ,.-");
          }
          return 0;
        }
        Output:


        Splitting string "- This, a sample string." into tokens:
        This
        a
        sample
        string

        Comment

        Working...