Print special characters in c that are in a.txt file with locale library. h

Asked

Viewed 1,246 times

4

My college late semester program has several screens that print large texts from arquivo.txt, however, as I do in c language, some characters do not appear, such as ç, é, ã...And so on and so forth.

I found a library called locale.h. With it you can write some commands that make your program search the language of your Operating System and print the special characters. In the Ubuntu, for example...The Brazilian locality is given by:pt_BR_utf8″.

Follows a code illustrative of my problem:

//Caracteres Especiais - Padrão PT/BR

`#include <locale.h>` 

//Biblioteca para definição de configuração local, necessario para o setlocale

    int main () 
    {
       printf("\nLocalidade Corrente: %s", setlocale(LC_ALL,NULL));
       printf("\nA localidade corrente agora  %s", setlocale(LC_ALL,""));
       //Com esse segundo printf, quer dizer que agora o programa está
       //habilitado a imprimir os caracteres especiais, certo?
       //Porém, eu imprimo o texto na função telaAjuda1() abaixo


       return(0);

    }

    void telaAjuda() {

    //Se o usuario digitar 1 aqui, o switch case vai chamar a função abaixo telaAjuda1(); - Numerei as funções, porque são 88. Achei mais fácil
    //de identificar mais tarde.

    }

    void telaAjuda1 ()
    {


        FILE *arq;
        char Linha[100];
        char *result;
        int i;
        system("clear");
        // Abre um arquivo TEXTO para LEITURA
        arq = fopen("telaAjuda1.txt", "rt");
        if (arq == NULL)  // Se houve erro na abertura
        {
            printf("Problemas na abertura do arquivo\n");
            return;
        }
        i = 1;
        while (!feof(arq))
        {
            // Lê uma linha (inclusive com o '\n')
            result = fgets(Linha, 100, arq);  // o 'fgets' lê até 99 caracteres ou até o '\n'
            if (result)  // Se foi possível ler
    //É no printf abaixo que imprimo o texto
                printf("\t%s",Linha);

            i++;
        }
        fclose(arq);

    }

NOTE: I thought maybe it could be error on my Ubuntu terminal that was not configured to read UTF-8 files, but went into settings and it is ok.

OBS 2: Below is an excerpt of text that is not printing correctly.

"The Data Structures course discusses several techniques of programming, presenting basic data structures used in software development.

Knowledge of programming languages alone does not enables programmers it is necessary to know how to use them from efficient way. The design of a program encompasses the phase identifying the properties of the data and functional features". - W. Celes and J. L. Rangel

The problem: Even if I look at the Ubuntu terminal, and even I put the locale. h and setting up I’m not understanding why it’s not printing properly.

1 answer

1


Did you notice if the file is on utf-8? I created a file here with your text and no need to include the library locale.h worked perfectly. Below is my test.

#include<stdio.h>

int main(){
    FILE *fp;
    char string[300];
    fp = fopen("file.txt","r");
    while(1){
        fgets(string,300,fp);
        if(feof(fp)) break;
        puts(string);
    }
    fclose(fp);
    printf("\n");
    return 0;
}

And the result obtained: Resultado

  • Thanks, that was the problem. I had to save in utf-8.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.