_cscanf, _cscanf_l, _cwscanf, _cwscanf_l

从控制台读取格式数据。 这些功能有更安全的版本可用;请参阅 _cscanf_s_cscanf_s_l_cwscanf_s_cwscanf_s_l

注意

在 Visual Studio 2015 中,printf 函数和 scanf 函数系列已声明为 inline 并移至 <stdio.h><conio.h> 标头。 如果正在迁移较旧的代码,可能会看到与这些函数相关的链接器错误 LNK2019。 有关详细信息,请参阅 Visual C++ 更改历史记录 2003 - 2015

重要

此 API 不能用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数

语法

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。 在操作系统命令行级别重定向键盘输入时,也可能返回 EOF。 返回值为零表示没有分配任何字段。

注解

_cscanf 函数直接将数据从控制台读取到 argument 给定的位置。 _getche 函数用于读取字符。 每个可选参数都必须为指向类型的变量的指针,该类型与 format 中的类型说明符对应。 格式控制输入字段的解释,其形式和功能与 scanf 函数的 format 参数相同。 虽然 _cscanf 通常回显输入字符,但如果最后一次调用的是 _ungetch,则不会执行此操作。

此函数验证其参数。 如果格式为 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
Enter three integers: 1 2 3
You entered 3 2 1

另请参阅

控制台和端口 I/O
_cprintf_cprintf_l_cwprintf_cwprintf_l
fscanf_fscanf_lfwscanf_fwscanf_l
scanf_s_scanf_s_lwscanf_s_wscanf_s_l
sscanf_sscanf_lswscanf_swscanf_l