vfscanf, vfwscanf


Reads formatted data from a stream. More secure versions of these functions are available; see vfscanf_s, vfwscanf_s.

int vfscanf( 
   FILE *stream,
   const char *format,
   va_list argptr 
int vfwscanf( 
   FILE *stream,
   const wchar_t *format,
   va_list argptr 


Pointer to FILE structure.


Format-control string.


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 are 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 and vfwscanf.

These functions validate their parameters. If stream 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.

The vfscanf function reads data from the current position of stream into the locations that are given by the arglist argument list. 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; see scanf for a description of format.

vfwscanf is a wide-character version of vfscanf; the format argument to vfwscanf is a wide-character string. These functions behave identically identically if the stream is opened in ANSI mode. vfscanf doesn't support input from a UNICODE stream.

Generic-Text Routine Mappings

TCHAR.H routine

_UNICODE & _MBCS not defined

_MBCS defined

_UNICODE defined





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



Required header




<stdio.h> or <wchar.h>

For additional compatibility information, see Compatibility.


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

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

FILE *stream;

int call_vfscanf(FILE * istream, char * format, ...)
    int result;
    va_list arglist;
    va_start(arglist, format);
    result = vfscanf(istream, format, arglist);
    return result;

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

    if (fopen_s(&stream, "vfscanf.out", "w+") != 0)
        printf("The file vfscanf.out was not opened\n");
        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(stream, "%s %ld %f%c", s, &l, &fp, &c);

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



.NET Framework Equivalent