vswscanf() Function in C/C++

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 :



// 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 :

// 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 GeekforGeeks";
  
    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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Practice Tags :
Article Tags :

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.






User Actions