_snscanf、_snscanf_l、_snwscanf、_snwscanf_l_snscanf, _snscanf_l, _snwscanf, _snwscanf_l

从字符串中读取指定长度的格式化数据。Reads formatted data of a specified length from a string. 提供这些函数的更多安全版本;请参阅 _snscanf_s、_snscanf_s_l、_snwscanf_s、_snwscanf_s_lMore secure versions of these functions are available; see _snscanf_s, _snscanf_s_l, _snwscanf_s, _snwscanf_s_l.

语法Syntax

int __cdecl _snscanf(  
   const char * input,  
   size_t length,  
   const char * format,  
   ...  
);  
int __cdecl _snscanf_l(  
   const char * input,  
   size_t length,  
   const char * format,  
   locale_t locale,  
   ...  
);  
int __cdecl _snwscanf(  
   const wchar_t * input,  
   size_t length,  
   const wchar_t * format,  
   ...  
);  
int __cdecl _snwscanf_l(  
   const wchar_t * input,  
   size_t length,  
   const wchar_t * format,  
   locale_t locale,  
   ...  
);  

参数Parameters

input
要检查的输入字符串。Input string to examine.

length
input 中要检查的字符数。Number of characters to examine in input.

format
一个或多个格式说明符。One or more format specifiers.

... (optional)
将用于存储通过 format 中的格式说明符从输入字符串中提取的值的变量。Variables that will be used to store the values extracted from the input string by the format specifiers in format.

locale
要使用的区域设置。The locale to use.

返回值Return Value

这两个函数都将返回已成功转换并分配的字段的数目;返回值不包括已读取但未分配的字段。Both of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. 返回值为 0 表示没有分配任何字段。A return value of 0 indicates that no fields were assigned. 如果在首次转换前到达字符串的结尾,则返回值为 EOF 以指示错误。The return value is EOF for an error or if the end of the string is reached before the first conversion. 有关详细信息,请参阅 sscanfFor more information, see sscanf.

如果 inputformat 是一个 NULL 指针,或如果 length 小于或等于 0,则将调用无效的参数处理程序,如参数验证中所述。If input or format is a NULL pointer, or if length is less than or equal to zero, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则这些函数将返回 EOF 并将 errno 设置为 EINVALIf execution is allowed to continue, these functions return EOF and set errno to EINVAL.

有关这些及其他错误代码的信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrFor information about these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

备注Remarks

此函数与 sscanf 类似,只不过它可以指定要在输入字符串检查的固定数量的字符。This function is like sscanf except that it provides the ability to specify a fixed number of characters to examine from the input string. 有关详细信息,请参阅 sscanfFor more information, see sscanf.

这些带有 _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
_sntscanf _snscanf _snscanf _snwscanf
_sntscanf_l _snscanf_l _snscanf_l _snwscanf_l

要求Requirements

例程Routine 必需的标头Required header
_snscanf, _snscanf_l_snscanf, _snscanf_l <stdio.h><stdio.h>
_snwscanf, _snwscanf_l_snwscanf, _snwscanf_l <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

有关更多兼容性信息,请参见“简介”中的 兼容性For more compatibility information, see Compatibility in the Introduction.

示例Example

// crt_snscanf.c  
// compile with: /W3  

#include <stdio.h>  
int main( )  
{  
   char  str1[] = "15 12 14...";  
   wchar_t  str2[] = L"15 12 14...";  
   char  s1[3];  
   wchar_t  s2[3];  
   int   i;  
   float fp;  

   i = _snscanf( str1, 6,  "%s %f", s1, &fp); // C4996  
   // Note: _snscanf is deprecated; consider using _snscanf_s instead  
   printf("_snscanf converted %d fields: ", i);  
   printf("%s and %f\n", s1, fp);  

   i = _snwscanf( str2, 6,  L"%s %f", s2, &fp); // C4996  
   // Note: _snwscanf is deprecated; consider using _snwscanf_s instead  
   wprintf(L"_snwscanf converted %d fields: ", i);  
   wprintf(L"%s and %f\n", s2, fp);  
}  
_snscanf converted 2 fields: 15 and 12.000000  
_snwscanf converted 2 fields: 15 and 12.000000  

请参阅See Also

scanf 宽度规范scanf Width Specification