The vswprintf() function is defined in <cwchar> header file.
vswprintf() prototype
int vswprintf( wchar_t* buffer, size_t buf_size, const wchar_t* format, va_list vlist );
The vswprintf() function writes the wide string pointed to by format to the wide string buffer. A maximum of
(buf_size-1) wide characters are written to buffer which is followed by a null wide character.
The wide string format may contain format specifiers starting with % which are replaced by the values of variables that are passed as a list vlist.
vswprintf() Parameters
- buffer: Pointer to a wide string buffer to write the result.
- buf_size: Maximum number of wide characters to write including the terminating null wide character.
- format: A pointer to a null terminated wide string that is written to buffer. It consists ofswprintf() Return value
- If successful, the vswprintf() function returns number of wide characters written excluding the terminating null wide character.
- A negative value is returned if any encoding error occurs or if the number of characters to be generated was equal or greater than buf_size.
Example: How vswprintf() function works?
#include <cwchar> #include <cstdarg> #include <clocale> void write(wchar_t* buffer, size_t buf_size, const wchar_t *fmt, ...) { va_list args; va_start(args, fmt); vswprintf(buffer, buf_size, fmt, args); va_end(args); } int main () { wchar_t str[] = L"\u0684 \u06b1 \u06aa \u06a3 \u0684"; wchar_t buffer[50]; setlocale(LC_ALL, "en_US.UTF-8"); write(buffer, 50, L"Arabic Letters: %ls\n", str); wprintf(L"%ls", buffer); return 0; }
When you run the program, the following will be written to example.txt:
Arabic Letters: ڄ ڱ ڪ ڣ ڄ