_cscanf_s、_cscanf_s_l、_cwscanf_s、_cwscanf_s_l_cscanf_s, _cscanf_s_l, _cwscanf_s, _cwscanf_s_l

从控制台读取格式数据。Reads formatted data from the console. 这些更安全版本的 _cscanf、_cscanf_l、_cwscanf、_cwscanf_l 具有安全增强功能,如 CRT 中的安全功能中所述。These more secure versions of _cscanf, _cscanf_l, _cwscanf, _cwscanf_l have security enhancements, as described in Security Features in the CRT.


此 API 不能用于在 Windows 运行时中执行的应用程序。This API cannot be used in applications that execute in the Windows Runtime. 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数For more information, see CRT functions not supported in Universal Windows Platform apps.


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-control string.

可选参数。Optional parameters.

要使用的区域设置。The locale to use.

返回值Return Value

已成功转换和分配的字段数。The number of fields that were successfully converted and assigned. 返回值不包括已读取但未分配的字段。The return value does not include fields that were read but not assigned. 返回值是EOF为尝试读取文件的末尾。The return value is EOF for an attempt to read at end of file. 在操作系统命令行级别重定向键盘输入时,会发生这种情况。This can occur when keyboard input is redirected at the operating-system command-line level. 返回值为 0 表示没有分配任何字段。A return value of 0 means that no fields were assigned.

这些函数验证其参数。These functions validate their parameters. 如果格式是 null 指针,这些函数将调用无效参数处理程序,如中所述参数验证If format is a null pointer, these functions invoke the invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,则这些函数将返回EOFerrno设置为EINVALIf execution is allowed to continue, these functions return EOF and errno is set to EINVAL.


_Cscanf_s函数先数据直接从控制台读取到提供的位置参数The _cscanf_s function reads data directly from the console into the locations given by argument. _Getche 函数用于读取字符。The _getche function is used to read characters. 每个可选参数必须是指向的变量中的类型说明符对应类型格式Each optional parameter must be a pointer to a variable with a type that corresponds to a type specifier in format. 输入的解释字段,并具有相同的格式控件形式和函数与格式参数scanf_s函数。The format controls the interpretation of the input fields and has the same form and function as the format parameter for the scanf_s function. 虽然 _cscanf_s通常回显输入的字符,不会这样执行操作,如果最后一次调用到 _ungetchWhile _cscanf_s normally echoes the input character, it does not do so if the last call was to _ungetch.

像其他安全版本中的函数的scanf系列, _cscanf_s_cswscanf_s需要类型字段字符的大小参数cCsS,和 [Like other secure versions of functions in the scanf family, _cscanf_s and _cswscanf_s require size arguments for the type field characters c, C, s, S, and [. 有关详细信息,请参阅 scanf 宽度规范For more information, see scanf Width Specification.


大小参数属于类型无符号,而不size_tThe size parameter is of type unsigned, not size_t.

使用这些函数的版本 _l后缀是相同,只不过它们使用传递而不是当前线程区域设置的区域设置参数。The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE and _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tcscanf_s_tcscanf_s _cscanf_s_cscanf_s _cscanf_s_cscanf_s _cwscanf_s_cwscanf_s
_tcscanf_s_l_tcscanf_s_l _cscanf_s_l_cscanf_s_l _cscanf_s_l_cscanf_s_l _cwscanf_s_l_cwscanf_s_l


例程Routine 必需的标头Required header
_cscanf_s_cscanf_s_l_cscanf_s, _cscanf_s_l <conio.h><conio.h>
_cwscanf_s_cwscanf_s_l_cwscanf_s, _cwscanf_s_l <conio.h> 或 <wchar.h><conio.h> or <wchar.h>

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.


C 运行时库的所有版本。All versions of the 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
You entered 1 2 3

请参阅See also

控制台和端口 I/OConsole and Port I/O
_cprintf、_cprintf_l、_cwprintf、_cwprintf_l_cprintf, _cprintf_l, _cwprintf, _cwprintf_l
fscanf_s、_fscanf_s_l、fwscanf_s、_fwscanf_s_lfscanf_s, _fscanf_s_l, fwscanf_s, _fwscanf_s_l
scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_lscanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l
sscanf_s、_sscanf_s_l、swscanf_s、_swscanf_s_lsscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l