INF: sscanf() Example Using a Comma (,) as Delimiter

ID Number: Q38335

5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

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.

More Information:

The following is sample C source code illustrating the use of brackets

and the caret (^):

Sample Code

-----------

/* Compile options needed: none

*/

#include <math.h>

#include <stdio.h>

char *tokenstring = "first,25.5,second,15";

int result, i;

double fp;

char o[10], f[10], s[10], t[10];

main()

{

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);

}

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00