The example below shows how to use the sscanf() C run-time function to read from an internal buffer delimiting fields with a comma (,). The key is to use the brackets in the format of sscanf() function. The format will be %[^','], which tells the function to read from the buffer until a comma (,) is reached.
/* The following sample illustrates the use of brackets and the
caret (^) with sscanf().
Compile options needed: none
char *tokenstring = "first,25.5,second,15";
int result, i;
char o, f, s, t;
result = sscanf(tokenstring, "%[^','],%[^','],%[^','],%s", o, s, t, f);
fp = atof(s);
i = atoi(f);
printf("%s\n %lf\n %s\n %d\n", o, fp, t, i);
Article ID: 38335 - Last Review: 17-Jun-2014 - Revision: 1