Tokenising Tokens..

This is a discussion on Tokenising Tokens.. within the C Programming forums, part of the General Programming Boards category; Hello there, Im been having some problems spliting my strings into tokens. I may be wrong here but to my ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    21

    Tokenising Tokens..

    Hello there,

    Im been having some problems spliting my strings into tokens. I may be wrong here but to my knowledge I cant get around the problem with sscanf because my strings have multiple spaces, etc.

    Here an an example string :

    Code:
    "A test.&Fred Smith+An Admin+AFK,Pete Smith+User+Active,A Nother+Admin+AFK&2003"
    The layout is like this : <MESSAGE>&<USERLIST>&<DATE>

    So what i need to is split my string firstly by the & symbol so Im left with A message, A User List and a Year.

    This data is arriving over a network so I would never know how many Users would be in the "User List" which are separated by commas.

    My problem was that I also needed to token the tokens aswell i.e the User List would contain <Name><Access><Status>. Because I was using strtok I couldnt perform another strtok while I was looping through and spliting the user strings via the ",". So I came up with this function which is very messy but it works. My question is could someone please look at my code and what ive done and advise if there is a better way of doing this...

    Ive tried to keep it short..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define DATA_STRING "A test.&Fred Smith+An Admin+AFK,Pete Smith+User+Active,A Nother+Admin+AFK&2003"
    
    void token_users(char *Name, char *Access, char *Status, char *String)
    {
     int i=0;
    
     do {
       if(*String != '+')
         Name[i++] = *String;
     } while (*String++ != '+');
    
     Name[i] = '\0';  // null terminate
     i=0;             // reset char counter
    
     do {
       if(*String != '+')
         Access[i++] = *String;
     } while (*String++ != '+');
    
     Access[i] = '\0';  // null terminate
     i=0;               // reset char counter
    
     do {
       if(*String != '\0')
         Status[i++] = *String;
     } while (*String++ != '\0');
    
     Status[i] = '\0';  // null terminate
    }
    
    int main(void)
    {
      char the_string[] = DATA_STRING;
      char *message, *people, *scratch = NULL;
      char Name[30],Access[30],Status[30]="";
      int year = 0;
    
      // do main split
      message = strtok(the_string,"&");
      people = strtok(NULL, "&");
      year = atoi(strtok(NULL, "&"));
    
      // display for debug
      printf("Message: %s\n", message);
      printf("People : %s\n", people);
      printf("Year   : %d\n\n", year);
    
      // split the first string and process
      scratch = strtok(people, ",");
      token_users(Name, Access, Status, scratch);
      printf("Name:%s\nAccess:%s\nStatus:%s\n\n", Name, Access, Status);
    
      // split the rest and process until there are no more
      while( (scratch = strtok(NULL, ",") ) != NULL)
      {
        token_users(Name, Access, Status, scratch);
        printf("Name:%s\nAccess:%s\nStatus:%s\n\n", Name, Access, Status);
      }
      
      printf("\n");
      return 0;
    }
    The output is like this :

    Code:
    Message: A test.
    People : Fred Smith+An Admin+AFK,Pete Smith+User+Active,A Nother+Admin+AFK
    Year   : 2003
    
    Name:Fred Smith
    Access:An Admin
    Status:AFK
    
    Name:Pete Smith
    Access:User
    Status:Active
    
    Name:A Nother
    Access:Admin
    Status:AFK
    If anyone has any pointers or words of advice i would appreciate, It took me ages to get it to work, but that function for splitting the user details looks very messy to me.

    Many Thanks

    Stanley.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       const char text[] = "A test.&Fred Smith+An Admin+AFK,Pete Smith+User+Active,A Nother+Admin+AFK&2003";
       char message [ sizeof text ], userlist [ sizeof text ], date [ sizeof text ];
       /* The layout is like this : <MESSAGE>&<USERLIST>&<DATE> */
       if ( sscanf(text, "%[^&]&%[^&]&%[^&]", message, userlist, date) == 3 )
       {
          char user [ sizeof userlist ];
          int i = 0, n = 0;
          printf("message  = \"%s\"\n", message);
          printf("userlist = \"%s\"\n", userlist);
          printf("date     = \"%s\"\n", date);
          while ( sscanf(&userlist[i], "%[^,],%n", user, &n) == 1 )
          {
             char name   [ sizeof user ];
             char access [ sizeof user ];
             char status [ sizeof user ];
             if ( sscanf(&userlist[i], "%[^+]+%[^+]+%[^,]", name, access, status) == 3 )
             {
                printf("\n name    = \"%s\"\n", name);
                printf(" access  = \"%s\"\n", access);
                printf(" status  = \"%s\"\n", status);
             }
             i += n;
          }
       }
       return 0;
    }
    
    /* my output
    message  = "A test."
    userlist = "Fred Smith+An Admin+AFK,Pete Smith+User+Active,A Nother+Admin+AFK"
    date     = "2003"
    
     name    = "Fred Smith"
     access  = "An Admin"
     status  = "AFK"
    
     name    = "Pete Smith"
     access  = "User"
     status  = "Active"
    
     name    = "A Nother"
     access  = "Admin"
     status  = "AFK"
    */
    Last edited by Dave_Sinkula; 06-04-2004 at 10:39 AM. Reason: Added color.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    Wow Dave, thanks alot, thats impressive and without using strtok at all I didnt know about all those special characters which can be used with sscanf, they seem very powerful.

    I will study sscanf and use this in my program from now on.

    Thanks again.

    Stanley

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-19-2008, 12:16 AM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  3. Splitting A String Into Tokens!
    By bobthebullet990 in forum C Programming
    Replies: 15
    Last Post: 03-22-2006, 08:24 AM
  4. How to output tokens in reverse order using strtok
    By Ninz in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 08:00 PM
  5. string tokens
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-16-2001, 11:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21