How do you use wprintf?

Hi, I am trying to display wide characters. Do any one know how to use wprintf?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#define _UNICODE
#define UNICODE

#include <windows.h>
#include <tchar.h>

int main()
{
	// The generic way
	TCHAR szName[] = _TEXT("Ragnampiza");
	_tprintf(_TEXT("My name is %s\n"), szName);

	// Harcoded unicode way
	wchar_t szAnotherName[] = L"Trinity";
	wprintf(L"My name is %s\n", szAnotherName);

	return 0;
}
Does Chinese count as wide characters? Because thats what I am trying to display, if not, is ther a function that can display Chinese and can you show me how to use it?
Last edited on
Yes, but I don't know if your console can render them. Disch is the expert on these matters.
Last I tried, I could not make the console to show a Unicode char even by using wcout. I know, it is not wprintf(). Still, I think you might be out of luck. If you want Chinese in your application, you'll have to go for one with a Windows GUI.
I know how to do it in GUI, so I want to know how to do it in console.
Actually, I once managed to get Kanji to display in my console...

To display these foreign characters you will need to change the 'system locale for non-unicode programs'.

This setting is in the control panel > Region and Language > Administrative tab > 'Change system locale...'

I think that it's likely anyone in China would have that setting on by default after installation.

So if you turn it on, then you'll see what they see and then you can figure out how to display your characters. I get the impression it might not be a wchar_t but an unsigned char that will display the new characters.

Good luck!
Use WriteConsoleW() WinAPI function and set Lucinda Console font in cmd.exe preferences.
There is no way to do it using standard library.
modoran: I beg to differ. Have you tried what I suggested earlier?
My computer's default language is already Chinese and how do you use WriteConsoleW()?
The problem here is that the term "wide character" is somewhat empty.

The standard library, as has been mentioned, will usually only output based on the current locale settings. At best, this will result in unreliable output if someone is running a locale setting other than what you intended. At worst it means you won't be able to print the characters you want at all.

This problem gets even worse when you realize that many wide output systems (like wcout) are worthless and simply narrow the characters before outputting them.

Bottom line: don't rely on the standard lib to output anything but basic ASCII.


My favored solution to this problem is to take advantage of Unicode. Unicode allows you to output any and all available characters. However... on Windows it means you can't use the standard library for console I/O. And even getting it to work without the standard lib takes some finagling.

Here's a thread from long ago which covered the topic:

http://www.cplusplus.com/forum/windows/9797/


Long story short, you need to do 2 things:

1) Set the console code page to Unicode (I typically prefer UTF-8)

2) Output Unicode via WriteConsole (if you're outputting UTF-8, you would use WriteConsoleA, not WriteConsoleW as you might expect)

Also note that UTF-8 does not use wide characters, so you can use normal chars. But make sure you notice that non-ascii characters will be multi-byte characters. IIRC most Chinese characters will be 3 or 4 bytes.

3) You'll need to have a font installed that can actually display these characters! Not all fonts have all characters. Incompatible fonts will draw some garbage character if they don't support it.

4) You'll need to make sure your console is using such a font. Since consoles are restricted on the fonts they can use, you'll probably have to get GNU Unifont and set it as your console's display font.

Here's code that should work in theory. I would test it, but unfortunately I can't get a proper Unicode font working with Windows console:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <Windows.h>
#include <cstring>
#include <iostream>

int main()
{
    // Step 1:  Set console to accept UTF-8 input/output
    SetConsoleCP(65001);
    SetConsoleOutputCP(65001);

    // Step 2:  A UTF-8 formatted string
    char example[] = "Some unicode chars:  ㅉㄆ伴伈佀傉僔";   // be sure to save the file as UTF-8 encoded (no signature, if possible)
    int len = strlen(example);

    // Step 3:  Output it
    WriteConsoleA( GetStdHandle(STD_OUTPUT_HANDLE), example, len, NULL, NULL );


    //stall
    std::cin.get();
}
Disch, I tried your solution and it came up as random characters in the console. Do ou have any other solutions?
I tried your solution and the chinese bit came up as random characters. Got any other solutions I could try?
Last edited on
Sorry =(

I would try to mess around with it more to figure it out, but without a Unicode font for the console I can't really do much. I even tried installing new fonts but I can't find one that will work with the console.
Topic archived. No new replies allowed.