You are currently offline, waiting for your internet to reconnect

How To sscanf() Example Using a Comma (,) as Delimiter

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
The example below shows how to use the sscanf() C run-time function toread from an internal buffer delimiting fields with a comma (,). Thekey is to use the brackets in the format of sscanf() function. Theformat will be %[^','], which tells the function to read from thebuffer until a comma (,) is reached.

Sample Code

/* The following sample illustrates the use of brackets and the   caret (^) with sscanf().   Compile options needed: none*/ #include <math.h>#include <stdio.h>#include <stdlib.h>char *tokenstring = "first,25.5,second,15";int result, i;double fp;char o[10], f[10], s[10], t[10];void 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);}

Article ID: 38335 - Last Review: 06/17/2014 21:43:00 - Revision: 4.0

  • kbcode kbcrt kbhowto KB38335