Microsoft KB Archive/38335

= How To sscanf Example Using a Comma as Delimiter =

Article ID: 38335

Article Last Modified on 7/13/2004

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 5.0 Standard Edition</li></ul>

 Microsoft Visual C++ 6.0 Service Pack 5</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q38335

<div class="notice_section">

<div class="summary_section">

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.

Sample Code
/* The following sample illustrates the use of brackets and the caret (^) with sscanf. Compile options needed: none


 * 1) include <math.h>
 * 2) include <stdio.h>
 * 3) include <stdlib.h>

char *tokenstring = &quot;first,25.5,second,15&quot;; int result, i; double fp; char o[10], f[10], s[10], t[10];

void main {  result = sscanf(tokenstring, &quot;%[^','],%[^','],%[^','],%s&quot;, o, s, t, f); fp = atof(s); i = atoi(f); printf(&quot;%s\n %lf\n %s\n %d\n&quot;, o, fp, t, i); }

Keywords: kbhowto kbcrt kbcode KB38335

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.