Muokkaa

Jaa


_snscanf_s, _snscanf_s_l, _snwscanf_s, _snwscanf_s_l

Reads formatted data of a specified length from a string. These functions are versions of _snscanf, _snscanf_l, _snwscanf, _snwscanf_l with security enhancements as described in Security features in the CRT.

Syntax

int __cdecl _snscanf_s(
   const char * input,
   size_t length,
   const char * format [, argument_list]
);
int __cdecl _snscanf_s_l(
   const char * input,
   size_t length,
   const char * format,
   _locale_t locale [, argument_list]
);
int __cdecl _snwscanf_s(
   const wchar_t * input,
   size_t length,
   const wchar_t * format [, argument_list]
);
int __cdecl _snwscanf_s_l(
   const wchar_t * input,
   size_t length,
   const wchar_t * format,
   _locale_t locale [, argument_list]
);

Parameters

input
Input string to examine.

length
Number of characters to examine in input.

format
One or more format specifiers.

locale
The locale to use.

argument_list
Optional arguments to be assigned according to the format string.

Return value

Both of these functions return the number of fields successfully converted and assigned; the return value doesn't include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion. For more information, see sscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l.

If input or format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter validation. If execution is allowed to continue, these functions return EOF and set errno to EINVAL.

For information about these and other error codes, see errno, _doserrno, _sys_errlist, and _sys_nerr.

Remarks

This function is like sscanf_s, except that it lets you specify a fixed number of characters to examine from the input string. For more information, see sscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l.

The buffer size parameter is required with the type field characters c, C, s, S, and [. For more information, see scanf Type Field Characters.

Note

The size parameter is of type unsigned, not size_t.

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 routine _UNICODE and _MBCS not defined _MBCS defined _UNICODE defined
_sntscanf_s _snscanf_s _snscanf_s _snwscanf_s
_sntscanf_s_l _snscanf_s_l _snscanf_s_l _snwscanf_s_l

Requirements

Routine Required header
_snscanf_s, _snscanf_s_l <stdio.h>
_snwscanf_s, _snwscanf_s_l <stdio.h> or <wchar.h>

For more compatibility information, see Compatibility.

Example

// crt_snscanf_s.c
// This example scans a string of
// numbers, using both the character
// and wide character secure versions
// of the snscanf function.

#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_s( str1, 6,  "%s %f", s1, 3, &fp);
    printf_s("_snscanf_s converted %d fields: ", i);
    printf_s("%s and %f\n", s1, fp);

    i = _snwscanf_s( str2, 6,  L"%s %f", s2, 3, &fp);
    wprintf_s(L"_snwscanf_s converted %d fields: ", i);
    wprintf_s(L"%s and %f\n", s2, fp);
}
_snscanf_s converted 2 fields: 15 and 12.000000
_snwscanf_s converted 2 fields: 15 and 12.000000

See also

scanf Width Specification