I am trying to read and manipulate Urdu text from files. However it seems that a character is not read whole into the wchar_t
variable. Here is my code that reads text and prints each character in a new line:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
void main(int argc, char* argv[]) {
setlocale(LC_ALL, "");
printf("This program tests Urdu reading:\n");
wchar_t c;
FILE *f = fopen("urdu.txt", "r");
while ((c = fgetwc(f)) != WEOF) {
wprintf(L"%lc\n", c);
}
fclose(f);
}
And here is my sample text:
میرا نام ابراھیم ھے۔
میں وینڈربلٹ یونیورسٹی میں پڑھتا ھوں۔
However there seem to be twice as many characters printed as there are letters in the text. I understand that wide or multi-byte characters use multiple bytes, but I thought that the wchar_t
type would store all the bytes corresponding to a letter in the alphabet together.
How can I read the text so that at any one time, I have a whole character stored in a variable?
Details about my environment:
gcc: (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 5.3.0
OS: Windows 10 64 bit
Text file encoding: UTF-8
This is how my text looks in hex format:
d9 85 db 8c d8 b1 d8 a7 20 d9 86 d8 a7 d9 85 20 d8 a7 d8 a8 d8 b1 d8 a7 da be db 8c d9 85 20 da be db 92 db 94 ad 98 5d b8 cd ab a2 0d 98 8d b8 cd 98 6d a8 8d 8b 1d 8a 8d 98 4d 9b 92 0d b8 cd 98 8d 98 6d b8 cd 98 8d 8b 1d 8b 3d 9b 9d b8 c2 0d 98 5d b8 cd ab a2 0d 9b ed a9 1d ab ed 8a ad 8a 72 0d ab ed 98 8d ab ad b9 4a