Share via


_cscanf、_cscanf_l、_cwscanf、_cwscanf_l

从控制台读取格式数据。 有关这些函数的更多安全版本,请参见 _cscanf_s、_cscanf_s_l、_cwscanf_s、_cwscanf_s_l

重要

此 API 不能用于在 Windows 运行时中执行的应用程序。有关详细信息,请参见 CRT functions not supported with /ZW(CRT 函数不支持使用/ZW)。

int _cscanf( 
   const char *format [,
   argument] ... 
);
int _cscanf_l( 
   const char *format,
   locale_t locale [,
   argument] ... 
);
int _cwscanf( 
   const wchar_t *format [,
   argument] ... 
);
int _cwscanf_l( 
   const wchar_t *format,
   locale_t locale [,
   argument] ... 
);

参数

  • format
    窗体控件字符串。

  • argument
    可选参数。

  • locale
    要使用的区域设置。

返回值

成功转换并分配字段数。 返回值不包括读取,但未赋值的字段。 返回值是EOF ,它尝试在文件末尾读取。 当键入被重定向的操作系统命令行级别时,该情况可能发生。 返回值为 0 表示未分配字段。

备注

_cscanf 函数直接从控制台读取数据到由argument给定的位置。 _getche 函数用于读取字符。 每个任意参数必须是指向类型变量的指针,此类型需与 format 中的类型说明符对应。 这种格式控制输入字段的解释,并且形式和函数都与 scanf 函数的format 参数相同。 _cscanf 正常回显输入字符,如果最后是对 _ungetch的调用,则不这样。

此函数验证其参数。 如果format为 NULL,则将调用无效参数处理程序,如参数验证所述 。 如果允许执行继续,errno设置为EINVAL,并且函数返回EOF。

这些带有 _l 后缀的函数的版本相同,只不过它们使用传递的区域设置参数而不是当前线程区域设置。

一般文本例程映射

TCHAR.H 例程

未定义 _UNICODE 和 _MBCS

已定义 _MBCS

已定义 _UNICODE

_tcscanf

_cscanf

_cscanf

_cwscanf

_tcscanf_l

_cscanf_l

_cscanf_l

_cwscanf_l

要求

例程

必需的标头

_cscanf,_cscanf_l

<conio.h>

_cwscanf, _cwscanf_l

<conio.h> 或 <wchar.h>

有关兼容性的更多信息,请参见兼容性

示例

// crt_cscanf.c
// compile with: /c /W3
/* This program prompts for a string
 * and uses _cscanf to read in the response.
 * Then _cscanf returns the number of items
 * matched, and the program displays that number.
 */

#include <stdio.h>
#include <conio.h>

int main( void )
{
   int   result, i[3];

   _cprintf_s( "Enter three integers: ");
   result = _cscanf( "%i %i %i", &i[0], &i[1], &i[2] ); // C4996
   // Note: _cscanf is deprecated; consider using _cscanf_s instead
   _cprintf_s( "\r\nYou entered " );
   while( result-- )
      _cprintf_s( "%i ", i[result] );
   _cprintf_s( "\r\n" );
}

输入

1 2 3

Output

Enter three integers: 1 2 3
You entered 3 2 1

请参见

参考

控制台和端口 I/O

_cprintf、_cprintf_l、_cwprintf、_cwprintf_l

fscanf、_fscanf_l、fwscanf、_fwscanf_l

scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_l

sscanf、_sscanf_l、swscanf、_swscanf_l