_cscanf_s, _cscanf_s_l, _cwscanf_s, _cwscanf_s_l
从控件个读取设置数据格式。 这些是 _cscanf, _cscanf_l, _cwscanf, _cwscanf_l 的更安全版本与安全增强如 CRT中的安全功能所述。
重要
此 API 不能在运行时的窗口执行的应用程序。有关更多信息,请参见 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 系列的,_cscanf_s 和 _cswscanf_s 为类型字段字符 c、C、s、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
请参见
参考
_cprintf, _cprintf_l, _cwprintf, _cwprintf_l
fscanf_s, _fscanf_s_l, fwscanf_s, _fwscanf_s_l