티스토리 뷰

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

Article ID : 38335
Last Review : July 13, 2004
Revision : 3.1
This article was previously published under Q38335
On This Page

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.

Back to the top

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

Back to the top

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함