sscanf() — 데이터 읽기

형식

#include <stdio.h>
int sscanf(const char *buffer, const char *format, argument-list);

언어 레벨

ANSI

스레드세이프

로케일 감지

이 함수의 작동은 현재 로케일의 LC_CTYPE 및 LC_NUMERIC 범주로 영향을 받을 수 있습니다. 또한 작동은 LOCALETYPE(*LOCALEUCS2) 또는 LOCALETYPE(*LOCALEUTF)이 컴파일 명령에 지정된 경우 현재 로케일의 LC_UNI_CTYPE 범주로 영향을 받을 수 있습니다. 자세한 정보는 CCSID 및 로케일 이해의 내용을 참조하십시오.

설명

sscanf() 함수는 buffer에서 argument-list가 제공하는 위치로 데이터를 읽습니다. 각 argumentformat-string에 서 유형 지정자에 대응하는 유형의 변수에 대한 포인터여야 합니다.

리턴값

sscanf() 함수는 성공적으로 변환 및 지정된 필드 수를 리턴합니다. 리턴값은 읽었지만 지정되지 않은 필드는 포함하지 않습니다.

리턴값은 변환 전에 스트링 끝이 나타난 경우 EOF입니다.

이 예는 sscanf()를 사용하여 스트링 tokenstring에서 다양한 데이터를 읽은 후 해당 데이터를 표시합니다.

#include <stdio.h>
#include <stddef.h>
 
int main(void)
{
    char    *tokenstring = "15 12 14";
    char    *string = "ABC Z";
    wchar_t  ws[81];
    wchar_t  wc;
    int i;
    float fp;
    char  s[81];
    char  c;

    /* Input various data                                        */
    /* In the first invocation of sscanf, the format string is   */
    /* "%s %c%d%f". If there were no space between %s and %c,    */
    /* sscanf would read the first character following the       */
    /* string, which is a blank space.                           */

    sscanf(tokenstring, "%s %c%d%f", s, &c, &i, &fp);
    sscanf(string, "%ls %lc", ws,&wc);

    /* Display the data */
    printf("\nstring = %s\n",s);
    printf("character = %c\n", c);
    printf("integer = %d\n", i);
    printf("floating-point number = %f\n",fp);
    printf("wide-character string = %S\n",ws);
    printf("wide-character = %C\n",wc);
}

/*****************  Output should be similar to:  *****************

string = 15
character = 1
integer = 2
floating-point number = 14.000000
wide-character string = ABC
wide-character = Z

*******************************************************************/