scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l

從標準輸入資料流讀取格式化資料。 這些版本的 scanf_scanf_lwscanf_wscanf_l 具有安全性增強功能,如 CRT 中的安全性功能中所述。

語法

int scanf_s(
   const char *format [,
   argument]...
);
int _scanf_s_l(
   const char *format,
   _locale_t locale [,
   argument]...
);
int wscanf_s(
   const wchar_t *format [,
   argument]...
);
int _wscanf_s_l(
   const wchar_t *format,
   _locale_t locale [,
   argument]...
);

參數

format
格式控制字串。

argument
選擇性引數。

locale
要使用的地區設定。

傳回值

傳回已成功轉換並指派的欄位數目。 傳回值不包含已讀取但未指派的欄位。 傳回值為 0 表示未指派任何欄位。 傳回值 EOF 適用于錯誤,或如果在第一次嘗試讀取字元時找到檔案結尾字元或字串結尾字元。 如果 formatNULL 指標,則會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,則 scanf_swscanf_s 會傳回 EOF,且 errno 設為 EINVAL

如需這些錯誤碼和其他錯誤碼的相關資訊,請參閱 errno_doserrno_sys_errlist_sys_nerr

備註

scanf_s 式會從標準輸入資料流程讀取資料, stdin 並將它 argument 寫入 。 每個 argument 都必須是對應至 中 format 型別規範之變數類型的指標。 如果在重疊的字串之間進行複製,則行為是未定義的。

wscanf_s 是寬字元版本的 scanf_sformatwscanf_s 引數是寬字元字串。 如果資料流在 ANSI 模式中開啟,則 wscanf_sscanf_s 的行為相同。 scanf_s 目前不支援來自 UNICODE 資料流的輸入。

具有 _l 尾碼的這些函式版本完全相同,不同之處在于它們會使用 locale 參數,而不是目前的執行緒地區設定。

與 和 wscanf 不同 scanfscanf_s 而且 wscanf_s 要求您指定某些參數的緩衝區大小。 指定所有 cC 、、 Ss 或 字串控制集 [] 參數的大小。 字元中的緩衝區大小會以另一個參數的形式傳遞。 它會緊接在緩衝區或變數的指標後面。 例如,如果您要讀取字串,則會傳遞該字串的緩衝區大小,如下所示:

char s[10];
scanf_s("%9s", s, (unsigned)_countof(s)); // buffer size is 10, width specification is 9

緩衝區大小包含終端機 Null。 您可以使用寬度規格欄位來確保讀取中的權杖符合緩衝區。 當令牌太大而無法容納時,除非有寬度規格,否則不會寫入緩衝區。

注意

大小參數為型別 unsigned,而非 size_t。 使用靜態轉型,以針對 64 位元組建組態,將 size_t 值轉換成 unsigned

緩衝區大小參數描述字元數目上限,而非位元組數。 在此範例中,緩衝區類型的寬度不符合格式規範的寬度。

wchar_t ws[10];
wscanf_s(L"%9S", ws, (unsigned)_countof(ws));

S格式規範表示使用函式所支援的預設寬度「相反」的字元寬度。 字元寬度是單一位元組,但函式支援雙位元組字元。 這個範例會以最多 9 個單位元組寬字元的字串讀取,並將它們放在雙位元組寬字元緩衝區中。 字元會視為單一位元組值;前兩個字元會儲存在 ws[0] 中,而後兩個會儲存在 ws[1] 中,依此類推。

此範例會讀取單一字元:

char c;
scanf_s("%c", &c, 1);

讀取非 Null 終止字串的多個字元時,整數會同時用於寬度規格和緩衝區大小。

char c[4];
scanf_s("%4c", c, (unsigned)_countof(c)); // not null terminated

如需詳細資訊,請參閱 scanf 寬度規格

泛型文字常式對應

TCHAR.H 常規 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_tscanf_s scanf_s scanf_s wscanf_s
_tscanf_s_l _scanf_s_l _scanf_s_l _wscanf_s_l

如需詳細資訊,請參閱 格式化規格欄位: scanfwscanf 函式

需求

常式 必要的標頭
scanf_s, _scanf_s_l <stdio.h>
wscanf_s, _wscanf_s_l <stdio.h><wchar.h>

通用 Windows 平臺 (UWP) 應用程式中不支援主控台。 標準資料流程控制碼 stdinstdoutstderr 必須先重新導向,C 執行時間函式才能在 UWP app 中使用它們。 如需相容性詳細資訊,請參閱相容性

範例

// crt_scanf_s.c
// This program uses the scanf_s and wscanf_s functions
// to read formatted input.

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
   int      i,
            result;
   float    fp;
   char     c,
            s[80];
   wchar_t  wc,
            ws[80];

   result = scanf_s( "%d %f %c %C %s %S", &i, &fp, &c, 1,
                     &wc, 1, s, (unsigned)_countof(s), ws, (unsigned)_countof(ws) );
   printf( "The number of fields input is %d\n", result );
   printf( "The contents are: %d %f %c %C %s %S\n", i, fp, c,
           wc, s, ws);
   result = wscanf_s( L"%d %f %hc %lc %S %ls", &i, &fp, &c, 2,
                      &wc, 1, s, (unsigned)_countof(s), ws, (unsigned)_countof(ws) );
   wprintf( L"The number of fields input is %d\n", result );
   wprintf( L"The contents are: %d %f %C %c %hs %s\n", i, fp,
            c, wc, s, ws);
}

此程式會在提供此輸入時產生下列輸出:

71 98.6 h z Byte characters
36 92.3 y n Wide characters
The number of fields input is 6
The contents are: 71 98.599998 h z Byte characters
The number of fields input is 6
The contents are: 36 92.300003 y n Wide characters

另請參閱

數學和浮點支援
資料流 I/O
地區設定
fscanf, _fscanf_l, fwscanf, _fwscanf_l
printf, _printf_l, wprintf, _wprintf_l
sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
sscanf, _sscanf_l, swscanf, _swscanf_l