What is vswscanf_s() in C?
vswscanf_s() reads a formatted list of arguments from a buffer; the vswscanf_s() function validates the arguments at runtime before the read operation. The declaration for vswscanf_s() is shown below:
int vswscanf_s( wchar_t *buffer, const wchar_t *format, va_list args);
Note: The
vswscan_sfunction is not supported by some compilers. If a compiler supportsvswscanf_s, the value ofSTDC_WANT_LIB_EXT1is set to 1.
Parameters
buffer: Pointer to the wide-character array from where the input is to be read.format: Format in which the input will be read.args: Pointer to the list of arguments in which data will be read.
Return value
If the read operation from the buffer is successful, vswscanf_s() returns the number of arguments that are read. If the read operation is not successful,
Example
Consider the code snippet below, which demonstrates the use of vswscanf_s():
#include <stdio.h>#include <wchar.h>#include <stdarg.h>int readformatted (const wchar_t * buffer, const wchar_t * format, ...){int len=0;va_list arg_list;va_start (arg_list, format);len = vswscanf_s (buffer, format, arg_list);va_end (arg_list);return len;}int main (){wchar_t buffer[80] = L"This is vswscanf Example code";wchar_t buff[5][200];int len = readformatted(buffer, L" %s %s %s %s %s %s", buff[0] , buff[1] , buff[2] , buff[3] , buff[4] );printf("Buffer Content: %s %s %s %s %s \n", buff[0] , buff[1] , buff[2] , buff[3] , buff[4] );printf("Number of arguments read from file: %d \n", len);return 0;}
Output
Buffer Content: This is vswscanf Example code
Number of arguments read from file: 5
Explanation
The readformatted() function in line 21 uses vswscanf_s() in line 10 to read the content of the buffer. The content of the buffer is written to buff, which is created in line 19.
Note: The function above is not supported by the GCC compiler, hence you will get an implicit declaration of function
vswscanf_serror. Use the following variant to get the job done:int vswscanf( wchar_t *buffer, const wchar_t *format, va_list args);.
Free Resources