Export (0) Print
Expand All
and
div
eof
not
or
xor
Expand Minimize
This topic has not yet been rated - Rate this topic

vsscanf, vswscanf

Visual Studio 2013

Reads formatted data from a string. More secure versions of these functions are available; see vsscanf_s, vswscanf_s.

int vsscanf(
   const char *buffer,
   const char *format,
   va_list arglist
);
int vswscanf(
   const wchar_t *buffer,
   const wchar_t *format,
   va_list arglist
);
buffer

Stored data

format

Format-control string. For more information, see Format Specification Fields: scanf and wscanf Functions.

arglist

Variable argument list.

Each of these functions returns the number of fields that are successfully converted and assigned; the return value does not 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.

If buffer 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 -1 and set errno to EINVAL.

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

The vsscanf function reads data from buffer into the locations that are given by each argument in the arglist argument list. Every argument in the list must be a pointer to a variable that has a type that corresponds to a type specifier in format. The format argument controls the interpretation of the input fields and has the same form and function as the format argument for the scanf function. If copying takes place between strings that overlap, the behavior is undefined.

Security note Security Note

When you use vsscanf to read a string, always specify a width for the %s format (for example, "%32s" instead of "%s"); otherwise, incorrectly formatted input can cause a buffer overrun.

vswscanf is a wide-character version of vsscanf; the arguments to vswscanf are wide-character strings. vsscanf does not handle multibyte hexadecimal characters. vswscanf does not handle Unicode full-width hexadecimal or "compatibility zone" characters. Otherwise, vswscanf and vsscanf behave identically.

Generic-Text Routine Mappings

TCHAR.H routine

_UNICODE & _MBCS not defined

_MBCS defined

_UNICODE defined

_vstscanf

vsscanf

vsscanf

vswscanf

Routine

Required header

vsscanf

<stdio.h>

vswscanf

<stdio.h> or <wchar.h>

For additional compatibility information, see Compatibility.

// crt_vsscanf.c
// compile with: /W3
// This program uses vsscanf to read data items
// from a string named tokenstring, then displays them.

#include <stdio.h>
#include <stdarg.h>

int call_vsscanf(char *tokenstring, char *format, ...)
{
    int result;
    va_list arglist;
    va_start(arglist, format);
    result = vsscanf(tokenstring, format, arglist);
    va_end(arglist);
    return result;
}

int main( void )
{
    char  tokenstring[] = "15 12 14...";
    char  s[81];
    char  c;
    int   i;
    float fp;

    // Input various data from tokenstring:
    // max 80 character string:
    call_vsscanf(tokenstring, "%80s", s);
    call_vsscanf(tokenstring, "%c", &c);
    call_vsscanf(tokenstring, "%d", &i);
    call_vsscanf(tokenstring, "%f", &fp);

    // Output the data read
    printf("String    = %s\n", s);
    printf("Character = %c\n", c);
    printf("Integer:  = %d\n", i);
    printf("Real:     = %f\n", fp);
}
String    = 15
Character = 1
Integer:  = 15
Real:     = 15.000000
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.