Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading unicode file in c

Tags:

c

file

unicode

I just want to read read unicode text file in normal c. Following code is not working for same,

#include<stdio.h>

int main()
{
        FILE *ptr_file;
        char buf[1000];

        ptr_file =fopen("input.txt","r");
        if (!ptr_file)
            return 1;

        while (fgets(buf,1000, ptr_file)!=NULL)
            printf("%s",buf);

    fclose(ptr_file);
        return 0;
}
like image 655
user1035089 Avatar asked Dec 06 '25 14:12

user1035089


1 Answers

Try this:

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

int main()
{
    FILE *input;
    wchar_t buf[1000];

    setlocale(LC_CTYPE,"it_IT.UTF-8");   // put your locale here

    if ((input = fopen("input.txt","r")) == NULL)
         return 1;

    while (fgetws(buf,1000,input)!=NULL) 
        wprintf(L"%s",buf);

    fclose(input);
}
like image 141
Antonio Caruso Avatar answered Dec 09 '25 03:12

Antonio Caruso



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!