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

4

My college end-of-school program has several screens that print large arquivo.txt , but as I do in C , some characters do not appear, such as ç, é, ã... And so on.

I discovered a library called locale.h . With it you can write commands that make your program search the language of your operating system and thereby print the special characters . In Ubuntu, for example ... The Brazilian location is given by: " pt_BR_utf8 ".

Here is an example 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 2: Below an excerpt of text that is not printing correctly.

"The Data Structures course discusses various programming, presenting the basic data structures used in software development.

Knowledge of programming languages alone does not enables programmers to know how to use them. efficient manner. The design of a program encompasses the identification of data properties and functional characteristics. "- W. Celes and J. L. Rangel

The problem: Even though I'm looking at the ubuntu terminal, and even putting the locale.h and configuring it, I'm not understanding why it's not printing correctly.

    
asked by anonymous 28.11.2015 / 20:42

1 answer

1

Did you notice if the file is in utf-8 ? I created a file here with your text and without needing 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:

    
29.11.2015 / 15:57