_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

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

lengthlength
要在中检查的字符数输入Number of characters to examine in input.

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

......
将用于存储中的格式说明符的输入字符串中提取的值的可选变量格式Optional variables that will be used to store the values extracted from the input string by the format specifiers in format.

localelocale
要使用的区域设置。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.

如果输入格式NULL指针,或如果长度小于或等于零,将调用无效参数处理程序,为中所述参数验证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并设置errnoEINVALIf 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_sntscanf _snscanf_snscanf _snscanf_snscanf _snwscanf_snwscanf
_sntscanf_l_sntscanf_l _snscanf_l_snscanf_l _snscanf_l_snscanf_l _snwscanf_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.

示例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