wscanf, fwscanf, swscanf, wscanf_s, fwscanf_s, swscanf_s

Header: <wchar.h>

Reads data from the a variety of sources, interprets it according to format and stores the results into given locations.

# Declarations

int wscanf( const wchar_t *format, ... );

(since C95) (until C99)

int wscanf( const wchar_t *restrict format, ... );

(since C99)

int fwscanf( FILE *stream, const wchar_t *format, ... );

(since C95) (until C99)

int fwscanf( FILE *restrict stream,
const wchar_t *restrict format, ... );

(since C99)

int swscanf( const wchar_t *buffer, const wchar_t *format, ... );

(since C95) (until C99)

int swscanf( const wchar_t *restrict buffer,
const wchar_t *restrict format, ... );

(since C99)

int wscanf_s( const wchar_t *restrict format, ...);

(since C11)

int fwscanf_s( FILE *restrict stream,
const wchar_t *restrict format, ...);

(since C11)

int swscanf_s( const wchar_t *restrict s,
const wchar_t *restrict format, ...);

(since C11)

# Parameters

# Example

#include <stdio.h>
#include <wchar.h>
#include <string.h>
 
#define NUM_VARS   3
#define ERR_READ   2
#define ERR_WRITE  3
 
int main(void) {
    wchar_t state[64];
    wchar_t capital[64];
    unsigned int population = 0;
    int elevation = 0;
    int age = 0;
    float pi = 0;
 
#if INTERACTIVE_MODE
    wprintf(L"Enter state, age, and pi value: ");
    if (wscanf(L"%ls%d%f", state, &age, &pi) != NUM_VARS) {
        fprintf(stderr, "Error reading input.\n");
        return ERR_READ;
    }
#else
    wchar_t* input = L"California 170 3.141592";
    if (swscanf(input, L"%ls%d%f", state, &age, &pi) != NUM_VARS) {
        fprintf(stderr, "Error reading input.\n");
        return ERR_READ;
    }
#endif
    wprintf(L"State: %ls\nAge  : %d years\nPi   : %.5f\n\n", state, age, pi);
 
    FILE* fp = tmpfile();
    if (fp) {
        // write some data to temp file
        if (!fwprintf(fp, L"Mississippi Jackson 420000 807")) {
            fprintf(stderr, "Error writing to file.\n");
            fclose(fp);
            return ERR_WRITE;
        }
        // rewind file pointer
        rewind(fp);
 
        // read data into variables
        fwscanf(fp, L"%ls%ls%u%d", state, capital, &population, &elevation);
        wprintf(L"State  : %ls\nCapital: %ls\nJackson population (in 2020): %u\n"
                L"Highest elevation: %dft\n",
                state, capital, population, elevation);
        fclose(fp);
    }
}

# See also