Language/C

sscanf

적외선 2012. 5. 29. 10:33

다음 예제에서는 구분 쉼표 (,) 필드와 내부 버퍼에서 읽을 sscanf() C 런타임 함수를 사용하는 방법을 보여 줍니다. 키가 sscanf() 함수 형식으로 대괄호를 사용합니다. 형식 % 됩니다 [^ ','], 있는 쉼표 (,) 도달할 때까지 버퍼에서 읽을 함수를 알려 줍니다.

예제 코드
/* 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);
}

출처 - http://support.microsoft.com/kb/38335/ko