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.