_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. 返回值为尝试在文件结尾读取的 EOFThe 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. 如果 format 为空指针,则这些函数将调用无效的参数处理程序,如参数验证中所述。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 函数直接将数据从控制台读取到 argument 给定的位置。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. 每个可选参数都必须为指向类型的变量的指针,该类型与 format 中的类型说明符对应。Each optional parameter must be a pointer to a variable with a type that corresponds to a type specifier in format. 格式控制输入字段的解释,格式和函数与 scanf_s 函数的 format 参数相同。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 通常回显输入字符,但如果最后一次调用的是 _ungetch,则不会执行此操作。While _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.


大小参数的类型具有 unsigned,而不具有 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 _cscanf_s _cscanf_s _cwscanf_s
_tcscanf_s_l _cscanf_s_l _cscanf_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/O Console 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_l fscanf_s, _fscanf_s_l, fwscanf_s, _fwscanf_s_l
scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_l scanf_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