std::wprintf, std::fwprintf, std::swprintf

Header: <cwchar>

Loads the data from the given locations, converts them to wide string equivalents and writes the results to a variety of sinks.

# Declarations

int wprintf( const wchar_t* format, ... );
int fwprintf( std::FILE* stream, const wchar_t* format, ... );
int swprintf( wchar_t* buffer, std::size_t size, const wchar_t* format, ... );

# Parameters

# Notes

While narrow strings provide std::snprintf, which makes it possible to determine the required output buffer size, there is no equivalent for wide strings, and in order to determine the buffer size, the program may need to call std::swprintf, check the result value, and reallocate a larger buffer, trying again until successful.

# Example

#include <clocale>
#include <cwchar>
#include <iostream>
#include <locale>
 
int main()
{
    char narrow_str[] = "z\u00df\u6c34\U0001f34c";
                  // or "zß水🍌";
                  // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9f\x8d\x8c";
    wchar_t warr[29]; // the expected string is 28 characters plus 1 null terminator
    std::setlocale(LC_ALL, "en_US.utf8");
 
    std::swprintf(warr, sizeof warr/sizeof *warr,
                  L"Converted from UTF-8: '%s'", narrow_str);
 
    std::wcout.imbue(std::locale("en_US.utf8"));
    std::wcout << warr << '\n';
}

# See also