Open In App

vswscanf() Function in C/C++

Last Updated : 06 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The vfwscanf() function in C++ is used to read formatted data from wide string into variable argument list. It also reads wide character string from a wide string buffer. This function reads data from ws and stores them according to format into the locations pointed by the elements in the variable argument list identified by arg. It is defined in library file. Syntax :

int vswscanf( const wchar_t* ws, const wchar_t* format, va_list arg )

Parameters : The function accepts three mandatory parameters which are described below:

  • ws :Pointer to the null terminated wide string to read the data
  • format :Pointer to a null-terminated wide character string that specifies how to read the input
  • arg : A value identifying a variable arguments list initialized with va_start.

Return value: The function returns two value as below:

  • on success, it returns the number of arguments successfully read.
  • on failure, EOF is returned

Below programs illustrate the above function: Program 1 : 

CPP




// C++ program to illustrate the
// vswscanf() function
#include <bits/stdc++.h>
using namespace std;
 
// ws : pointer to the wide string
// format : to read the input
void wideMatch(const wchar_t* ws, const wchar_t* format, ...)
{
    va_list arg;
 
    // A function that invokes va_start
    // shall also invoke va_end before it returns.
    va_start(arg, format);
 
    // vswscanf() reads formatted data from wide
    // string into variable argument list
    vswscanf(ws, format, arg);
    va_end(arg);
}
 
// Driver code
int main()
{
    setlocale(LC_ALL, "en_US.UTF-8");
 
    // initialize the buffer
    wchar_t wideS[] = L"GFG";
    wchar_t string[20];
 
    wideMatch(wideS, L"%ls", string);
    wprintf(L"Random Symbols are :\n");
 
    // print all the symbols
    for (int i = 0; i < wcslen(string); i++) {
        putwchar(string[i]);
        putwchar(' ');
    }
 
    return 0;
}


Output:

Random Symbols are :
G F G

Program 2 : 

CPP




// C++ program to illustrate the
// vswscanf() function
 
#include <bits/stdc++.h>
using namespace std;
 
void WideString(const wchar_t* ws, const wchar_t* format, ...)
{
    va_list arg;
    // A function that invokes va_start
    // shall also invoke va_end before it returns.
    va_start(arg, format);
 
    // vswscanf() reads formatted data from wide
    // string into variable argument list
    vswscanf(ws, format, arg);
    va_end(arg);
}
 
// Driver code
int main()
{
    int value;
 
    // initialize the buffer
    wchar_t wideS[] = L"100 websites of GeeksforGeeks";
 
    WideString(wideS, L" %d %ls ", &value, wideS);
 
    // print all the symbols
    wprintf(L"Best: %ls\nQuantity: %d\n", wideS, value);
 
    return 0;
}


Output:

Best: websites
Quantity: 100


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads