Export (0) Print
Expand All
and
div
eof
not
or
xor
Expand Minimize

vfscanf_s, vfwscanf_s

Visual Studio 2013

Reads formatted data from a stream. These versions of vfscanf, vfwscanf have security enhancements, as described in Security Features in the CRT.

int vfscanf_s( 
   FILE *stream,
   const char *format,
   va_list arglist
);
int vfwscanf_s( 
   FILE *stream,
   const wchar_t *format,
   va_list arglist
);

stream

Pointer to FILE structure.

format

Format-control string.

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. If an error occurs, or if the end of the file stream is reached before the first conversion, the return value is EOF for vfscanf_s and vfwscanf_s.

These functions validate their parameters. If stream is an invalid file pointer, or format is a null pointer, these functions invoke the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, these functions return EOF and set errno to EINVAL.

The vfscanf_s function reads data from the current position of stream into the locations that are given by the arglist argument list (if any). Each argument in the list must be a pointer to a variable of a type that corresponds to a type specifier in format. format controls the interpretation of the input fields and has the same form and function as the format argument for scanf_s; see Format Specification Fields: scanf and wscanf Functions for a description of format. vfwscanf_s is a wide-character version of vfscanf_s; the format argument to vfwscanf_s is a wide-character string. These functions behave identically if the stream is opened in ANSI mode. vfscanf_s doesn't currently support input from a UNICODE stream.

The main difference between the more secure functions (that have the _s suffix) and the other versions is that the more secure functions require the size in characters of each c, C, s, S, and [ type field to be passed as an argument immediately following the variable. For more information, see scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l and scanf Width Specification.

Note Note

The size parameter is of type unsigned, not size_t.

Generic-Text Routine Mappings

TCHAR.H routine

_UNICODE & _MBCS not defined

_MBCS defined

_UNICODE defined

_vftscanf_s

vfscanf_s

vfscanf_s

vfwscanf_s

Function

Required header

vfscanf_s

<stdio.h>

vfwscanf_s

<stdio.h> or <wchar.h>

For additional compatibility information, see Compatibility.

// crt_vfscanf_s.c
// compile with: /W3
// This program writes formatted
// data to a file. It then uses vfscanf_s to
// read the various data back from the file.

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

FILE *stream;

int call_vfscanf_s(FILE * istream, char * format, ...)
{
    int result;
    va_list arglist;
    va_start(arglist, format);
    result = vfscanf_s(istream, format, arglist);
    va_end(arglist);
    return result;
}

int main(void)
{
    long l;
    float fp;
    char s[81];
    char c;

    if (fopen_s(&stream, "vfscanf_s.out", "w+") != 0)
    {
        printf("The file vfscanf_s.out was not opened\n");
    }
    else
    {
        fprintf(stream, "%s %ld %f%c", "a-string",
            65000, 3.14159, 'x');
        // Security caution!
        // Beware loading data from a file without confirming its size,
        // as it may lead to a buffer overrun situation.

        // Set pointer to beginning of file:
        fseek(stream, 0L, SEEK_SET);

        // Read data back from file:
        call_vfscanf_s(stream, "%s %ld %f%c", s, _countof(s), &l, &fp, &c, 1);

        // Output data read: 
        printf("%s\n", s);
        printf("%ld\n", l);
        printf("%f\n", fp);
        printf("%c\n", c);

        fclose(stream);
    }
}
a-string
65000
3.141590
x
Show:
© 2014 Microsoft