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 で指定された位置へ、データを読み取ります。 各 argument は、 format-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

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