How to Print the Wchar_T Values to Console

How I can print the wchar_t values to console?

Edit: This doesn’t work if you are trying to write text that cannot be represented in your default locale. :-(

Use std::wcout instead of std::cout.

wcout << ru << endl << en;

How do you print a whole wchar_t with spaces at once? (c++)

I see the question is evolved from getting 1 char at a time problem to getting 1 word at a time problem. You can use fgetws to capture the whole input:

while (1)
{
wchar_t afterprint[100];
std::wcout << "\n>>> ";
fgetws(afterprint, 100, stdin);
std::wcout << afterprint;
}

Print wchar to Linux console?

This was quite interesting. Apparently the compiler translates the omega from UTF-8 to UNICODE but somehow the libc messes it up.

First of all: the %c-format specifier expects a char (even in the wprintf-version) so you have to specify %lc (and therefore %ls for strings).

Secondly if you run your code like that the locale is set to C (it isn't automatically taken from the environment). You have to call setlocale with an empty string to take the locale from the environment, so the libc is happy again.

#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <locale.h>

int main() {
int r;
wchar_t myChar1 = L'Ω';
setlocale(LC_CTYPE, "");
r = wprintf(L"char is %lc (%x)\n", myChar1, myChar1);
}

Memory leak issue with printing wchar_t to console?

When you say *ptr you dereference the pointer and access the first element of it. If animalID is a wchart_t* then just assigning that to myAnimalID should be enough.

std::wstring myAnimalID = animal->second ->animalID;

C - wchar_t prints unwanted characters

DIR *dir is for ANSI, not Unicode. Use _WDIR instead.

Windows has limited support for printing Unicode. In MinGW use WriteConsoleW for printing Unicode. Example:

#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <windows.h>

void myprint(const wchar_t* str)
{
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wcslen(str), NULL, NULL);
}

void listFolder(const wchar_t* path)
{
_WDIR *dir = _wopendir(path);
if (!dir) return;

struct _wdirent *dp;
while ((dp=_wreaddir(dir)) != NULL)
{
if ( wcscmp(dp->d_name, L".") == 0 || wcscmp(dp->d_name, L"..") == 0)
continue;
myprint(dp->d_name);
wprintf(L"\n");
}
_wclosedir(dir);
}


Related Topics



Leave a reply



Submit