_cscanf_s、_cscanf_s_l、_cwscanf_s、_cwscanf_s_l

从控制台读取格式数据。 _cscanf、_cscanf_l、_cwscanf、_cwscanf_l 的这些版本如 CRT 中的安全功能 所述,其安全得到了增强。

重要

此 API 不能用于在 Windows 运行时中执行的应用程序。有关详细信息,请参见 CRT functions not supported with /ZW(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 系列中类似于函数的其他安全版本, 对于类型字段字符c、C、s、S和 [,_cscanf_s 和 _cswscanf_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

Output

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