Joe Caraccio
Joe Caraccio

Reputation: 2035

Reading Files in from UTF8

I am trying to reading in a file that contains characters in UTF-8 format.

    FILE * f = fopen(argv[1], "r");
    if(f == NULL){
            printf("cannot open %s\n", argv[1]);
            exit(-1);
        }
    unsigned int c = getc();
    while(c != EOF){
        printf("%d\n", c);  // UB
        c = getchar();

    }

How do I read the files in so that they are in bit representation? For example that it would look like: 0xA3. Right now it's printing actual integers. Basically, how do I read in characters in bit wise format?

Upvotes: 2

Views: 143

Answers (1)

rajesh6115
rajesh6115

Reputation: 735

sample Example for utf-8 file reading.....

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

int main(int argc, char *argv[]){
        setlocale(LC_ALL, "en_US.UTF-8");
        FILE * f = fopen(argv[1], "r");
        if(f == NULL){
                printf("cannot open %s\n", argv[1]);
                exit(-1);
        }
        wchar_t wc;
        while((wc=fgetwc(f))!=WEOF){
                //wprintf(L"%lc", wc);
                // for output as hex of uthf-8 characters
                wprintf(L"0X%X,",(wint_t) wc);
        }
        wprintf(L"\n");
        fclose(f);
        return 0;
}

Upvotes: 2

Related Questions