📜  C / C++中的vswscanf()函数

📅  最后修改于: 2021-05-30 13:39:07             🧑  作者: Mango

C++中的vfwscanf()函数用于将格式化的数据从宽字符串读取到变量参数列表中。它还读取一个宽字符串缓冲区宽。此函数从ws读取数据,并根据格式将其存储到arg所标识的变量参数列表中的元素所指向的位置。
它在库文件。

句法 :

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

参数:该函数接受三个强制性参数,如下所述:

  • ws:指向以null终止的宽字符串的指针,以读取数据
  • 格式:指向一个空终止宽指定如何读取输入
  • arg:一个值,该值标识用va_start初始化的变量参数列表。

返回值:该函数返回两个值,如下所示:

  • 成功时,它返回成功读取的参数数量。
  • 如果失败,则返回EOF

下面的程序说明了上述函数:
程序1:

// C++ program to illustrate the
// vswscanf() function
#include 
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;
}
输出:
Random Symbols are :
G F G

程序2:

// C++ program to illustrate the
// vswscanf() function
  
#include 
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;
}
输出:
Best: websites
Quantity: 100
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。