You need to do two things.
- Make sure your source file is UTF-8 with BOM.
- Call either
_setmode(filedescriptor, _O_U16TEXT);
or _setmode(filedescriptor, _O_U8TEXT);
before doing any output.
The choice of mode depends on whether you want UTF-8 or UTF-16 output. Most of the time you want UTF-8 if you are writing to a disk file, and UTF-16 if you are writing to the console. Why, isn't this system beautiful?
To obtain the file descriptor for wfstream yourstream
, use yourstream.fd()
.
To obtain the file descriptor for stdout
, use _fileno(stdout)
.
The console may or may not support Arabic. See here for more info. You should always be able to write to a file though.
You need to include additional headers:
#include <io.h>
#include <fcntl.h>
Note, this is specific to the Microsoft compiler.
Edit: added the discussion of different modes.