_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.

语法Syntax

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] ...
);

参数Parameters

formatformat
窗体控件字符串。Format-control string.

自变量argument
可选参数。Optional parameters.

localelocale
要使用的区域设置。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. 如果格式是空指针,这些函数将调用无效参数处理程序,如中所述参数验证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.

备注Remarks

_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

要求Requirements

例程所返回的值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.

Libraries

C 运行时库的所有版本。All versions of the C run-time libraries.

示例Example

// 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