_cscanf_s、 _cscanf_s_l、 _cwscanf_s、 _cwscanf_s_l
從主控台讀取格式化資料。 這些是 _cscanf、 _cscanf_l、 _cwscanf、 _cwscanf_l 更加安全版本與安全性增強的如 安全性功能,則在 CRT 中中所述。
重要
這個 API 不能用於 Windows 執行階段執行的應用程式。如需詳細資訊,請參閱 CRT 函式不支援使用 /ZW。
int _cscanf_s(
const char *format [,
argument] ...
);
int _cscanf_s_l(
const char *format,
locale_t locale [,
argument] ...
);
int _cwscanf_s(
const wchar_t *format [,
argument] ...
);
int _cwscanf_s_l(
const wchar_t *format,
locale_t locale [,
argument] ...
);
參數
format
格式控制字串。argument
選擇性參數。locale
使用的地區設定。
傳回值
已成功轉換和指定欄位的數目。 傳回值不包括讀取,但未指定的欄位。 傳回值是嘗試的 EOF 可以讀取檔案的結尾。 當輸入重新導向在作業系統命令列層級時,就會發生。 傳回值 0 表示欄位未指定。
這些函式會驗證它們的參數。 如果 format 為 null 指標,這些函式叫用無效的參數處理常式,如 參數驗證中所述。 如果執行允許繼續執行,這些函式傳回 EOF 和 errno設定為 EINVAL。
備註
_cscanf_s 函式會將資料直接從主控台輸入 argument指定的位置。 _getche 函式用來讀取字元。 每個選擇性參數必須是指標與對應至 format的型別規範的型別的變數。 輸入說明欄位且具有表單和函式和 scanf_s 函式的 format 參數的格式控制。 當 _cscanf_s 通常回應輸入字元時,它不這樣做,如果最後一次呼叫是 _ungetch。
就像其他函式的安全版本在scanf 參數的_cscanf_s 和 _cswscanf_s ,為這個型別欄位字元 c、 C、 s、 S和 [要求大小引數。 如需詳細資訊,請參閱scanf 寬度規格。
![]() |
---|
大小參數是 unsigned型別,也不是 size_t。 |
這些函式的以 _l 後綴版本相同,但使用傳遞的地區設定參數而非目前執行緒的地區設定。
泛用文字常式對應
TCHAR.H 常式 |
未定義的 _UNICODE 和 _MBCS |
已定義 _MBCS |
已定義 _UNICODE |
---|---|---|---|
_tcscanf_s |
_cscanf_s |
_cscanf_s |
_cwscanf_s |
_tcscanf_s_l |
_cscanf_s_l |
_cscanf_s_l |
_cwscanf_s_l |
需求
程序 |
必要的標頭檔 |
---|---|
_cscanf_s,_cscanf_s_l |
<conio.h> |
_cwscanf_s, _cwscanf_s_l |
<conio.h> 或 <wchar.h> |
如需更多關於相容性的資訊,請參閱入門介紹中的 相容性 (Compatibility) 。
程式庫
所有的 C 執行階段程式庫 (C run-time libraries) 版本。
範例
// crt_cscanf_s.c
// compile with: /c
/* This program prompts for a string
* and uses _cscanf_s to read in the response.
* Then _cscanf_s returns the number of items
* matched, and the program displays that number.
*/
#include <stdio.h>
#include <conio.h>
int main( void )
{
int result, n[3];
int i;
result = _cscanf_s( "%i %i %i", &n[0], &n[1], &n[2] );
_cprintf_s( "\r\nYou entered " );
for( i=0; i<result; i++ )
_cprintf_s( "%i ", n[i] );
_cprintf_s( "\r\n" );
}
輸入
1 2 3
Output
You entered 1 2 3
請參閱
參考
_cprintf、 _cprintf_l、 _cwprintf、 _cwprintf_l
fscanf_s、 _fscanf_s_l、 fwscanf_s、 _fwscanf_s_l