_cscanf_s
、 、 _cscanf_s_l
、 _cwscanf_s
_cwscanf_s_l
從主控台讀取格式化資料。 這些更安全的_cscanf
、 _cscanf_l
、 _cwscanf
_cwscanf_l
版本具有安全性增強功能,如 CRT 的安全性功能中所述。
重要
這個 API 不能用於在 Windows 執行階段中執行的應用程式。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 Windows 平台應用程式中不支援的 CRT 函式)。
語法
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
。 EOF
當鍵盤輸入在作業系統命令列層級重新導向時,也可以傳回 。 零的傳回值表示未指派任何欄位。
這些函式會驗證它們的參數。 如果 format
為 Null 指標,這些函式會叫用無效的參數處理程式,如參數驗證中所述。 如果允許繼續執行,這些函式會傳回 EOF
,並將 errno
設定為 EINVAL
。
備註
_cscanf_s
函式會將資料直接從主控台讀入 argument
指定的位置。 函 _getche
式是用來讀取字元。 每個選擇性參數都必須是其類型對應至 format
中類型規範的變數指標。 格式會控制輸入欄位的解譯,而且其形式和函式與 format
函式的參數 scanf_s
相同。 雖然 _cscanf_s
通常會回應輸入字元,但如果最後一個呼叫是 , _ungetch
則不會這麼做。
如同系列中的其他scanf
安全函式版本,_cscanf_s
而且_cwscanf_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> |
如需相容性詳細資訊,請參閱相容性。
程式庫
所有版本的 C 執行階段程式庫。
範例
// 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
You entered 1 2 3
另請參閱
主控台和連接埠 I/O
_cprintf
、 、 _cprintf_l
、 _cwprintf
_cwprintf_l
fscanf_s
、 、 _fscanf_s_l
、 fwscanf_s
_fwscanf_s_l
scanf_s
、 、 _scanf_s_l
、 wscanf_s
_wscanf_s_l
sscanf_s
、 、 _sscanf_s_l
、 swscanf_s
_swscanf_s_l