scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s
| ヘッダ <stdio.h> で定義
|
||
| (1) | ||
int scanf( const char *format, ... ); |
(C99未満) | |
int scanf( const char *restrict format, ... ); |
(C99以上) | |
| (2) | ||
int fscanf( FILE *stream, const char *format, ... ); |
(C99未満) | |
int fscanf( FILE *restrict stream, const char *restrict format, ... ); |
(C99以上) | |
| (3) | ||
int sscanf( const char *buffer, const char *format, ... ); |
(C99未満) | |
int sscanf( const char *restrict buffer, const char *restrict format, ... ); |
(C99以上) | |
int scanf_s(const char *restrict format, ...); |
(4) | (C11以上) |
int fscanf_s(FILE *restrict stream, const char *restrict format, ...); |
(5) | (C11以上) |
int sscanf_s(const char *restrict buffer, const char *restrict format, ...); |
(6) | (C11以上) |
様々なソースからデータを読み込み、それを format に従って解釈し、結果を指定された位置に格納します。
stream からデータを読み込みます。buffer からデータを読み込みます。 文字列の終端への到達は fscanf に対するファイル終端への到達と同等です。%c、 %s および %[ 変換指定子はそれぞれ2つの引数 (通常のポインタと、受け取り配列のサイズを表す rsize_t 型の値 (%c で単一の文字を読み込むときは 1 になるかもしれません)) を期待し、以下のエラーを実行時に検出して現在設定されている制約ハンドラ関数を呼びます。
- ポインタ型の引数のいずれかがヌルポインタ。
format、streamまたはbufferがヌルポインタ。- %c、 %s または %[ によって書き込まれるであろう文字数プラス終端のヌル文字が、それらの変換指定子に対して提供された2番目の (rsize_t 型の) 引数を超える。
- オプションで、不明な変換指定子などのその他の検出可能なあらゆるエラー。
- すべての境界チェック付き関数と同様に、
scanf_s、fscanf_s、 およびsscanf_sは__STDC_LIB_EXT1__が処理系によって定義されていて、<stdio.h>をインクルードする前にユーザが__STDC_WANT_LIB_EXT1__を整数定数 1 に定義した場合にのみ、利用可能であることが保証されます。
引数
| stream | - | 読み込む入力ファイルストリーム | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| buffer | - | 読み込むヌル終端文字列を指すポインタ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| format | - | 入力の読み込み方法を指定するヌル終端文字列を指すポインタ。
書式文字列は以下から構成されます。
以下の書式指定子が利用できます。
長さ指定子 変換指定子 固定幅の整数型に対する正しい変換指定は、ヘッダ <inttypes.h> で定義されています (SCNdMAX, SCNuMAX などは 変換指定子それぞれの動作後に副作用完了点があります。 これにより同じ「ゴミ箱」変数に複数のフィールドを格納することができます。 数字のない指数で終わる不完全な浮動小数点値をパースするとき、例えば変換指定子 変換指定が無効な場合、動作は未定義です。
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ... | - | 受け取り引数 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
戻り値
ノート
ほとんどの変換指定子は、まずすべての連続するホワイトスペースを消費します。 以下のようなコードは
scanf("%d", &a);
scanf("%d", &b);
別の行に入力された2つの整数 (この場合、2番目の %d は1番目の %d によって残された改行を消費します)、または同じ行の空白またはタブで区切られた2つの整数 (この場合、2番目の %d は空白またはタブを消費します) を読み込みます。
%c などの先行するホワイトスペースを消費しない変換指定子は、書式文字列内でホワイトスペース文字を使用することでホワイトスペースを消費するようにできます。
std::scanf("%d", &a);
std::scanf(" %c", &c); // ignore the endline after %d, then read a char
例
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <stddef.h>
#include <locale.h>
int main(void)
{
int i, j;
float x, y;
char str1[10], str2[4];
wchar_t warr[2];
setlocale(LC_ALL, "en_US.utf8");
char input[] = "25 54.32E-1 Thompson 56789 0123 56ß水";
/* parse as follows:
%d: an integer
%f: a floating-point value
%9s: a string of at most 9 non-whitespace characters
%2d: two-digit integer (digits 5 and 6)
%f: a floating-point value (digits 7, 8, 9)
%*d: an integer which isn't stored anywhere
' ': all consecutive whitespace
%3[0-9]: a string of at most 3 decimal digits (digits 5 and 6)
%2lc: two wide characters, using multibyte to wide conversion */
int ret = sscanf(input, "%d%f%9s%2d%f%*d %3[0-9]%2lc",
&i, &x, str1, &j, &y, str2, warr);
printf("Converted %d fields:\ni = %d\nx = %f\nstr1 = %s\n"
"j = %d\ny = %f\nstr2 = %s\n"
"warr[0] = U+%x warr[1] = U+%x\n",
ret, i, x, str1, j, y, str2, warr[0], warr[1]);
#ifdef __STDC_LIB_EXT1__
int n = sscanf_s(input, "%d%f%s", &i, &x, str1, (rsize_t)sizeof str1);
// writes 25 to i, 5.432 to x, the 9 bytes "thompson\0" to str1, and 3 to n.
#endif
}
出力:
Converted 7 fields:
i = 25
x = 5.432000
str1 = Thompson
j = 56
y = 789.000000
str2 = 56
warr[0] = U+df warr[1] = U+6c34
参考文献
- C11 standard (ISO/IEC 9899:2011):
- 7.21.6.2 The fscanf function (p: 317-324)
- 7.21.6.4 The scanf function (p: 325)
- 7.21.6.7 The sscanf function (p: 326)
- K.3.5.3.2 The fscanf_s function (p: 592-593)
- K.3.5.3.4 The scanf_s function (p: 594)
- K.3.5.3.7 The sscanf_s function (p: 596)
- C99 standard (ISO/IEC 9899:1999):
- 7.19.6.2 The fscanf function (p: 282-289)
- 7.19.6.4 The scanf function (p: 290)
- 7.19.6.7 The sscanf function (p: 291)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.9.6.2 The fscanf function
- 4.9.6.4 The scanf function
- 4.9.6.6 The sscanf function
関連項目
(C99)(C99)(C99)(C11)(C11)(C11) |
stdin、ファイルストリームまたはバッファから可変個引数リストを使用して書式付き入力を読み取ります (関数) |
| ファイルストリームから文字列を取得します (関数) | |
(C99)(C11)(C11)(C11)(C11) |
stdout、ファイルストリームまたはバッファに書式付き出力を書き出します (関数) |
scanf, fscanf, sscanf の C++リファレンス
| |