0

I am doing work in C to encrypt according to certain algorithms.

Everything is fine except if you have to use characters like ç or Ç.

Setlocale is set, and from the tests I did, the problem is no gets ().

Can anyone help me solve the problem?

This is only a function setlocale is on main function.

I have tried with printf to see where the problem might come from, so I came to the conclusion I explain above.

In other words, gets () is not assimilating the characters ç or Ç but as a symbol in another language mode.

system("cls");

FILE *nfile_1;

char tfile[2000];

printf("Opção Escolhida: (2) | Refazer Texto Do Ficheiro Pre-Conversão\n\n");
printf("____________________________________________________________________________\n\n");

printf("A mensagem atual contida no ficheiro 'mensagem.txt' será apresentada abaixo:\n\n\n\n");

nfile_1 = fopen("mensagem.txt", "r");

if(nfile_1 != NULL)
{
    while(fgets(tfile, 2000, nfile_1) != NULL)
    {
        printf("%s\n\n", tfile);
    }
}
else if(nfile_1 == NULL)
{
    printf("O Ficheiro 'mensagem.txt' Não Existe!\n\n");
}

fclose(nfile_1);

printf("\n\n----------------------------------------------------------------------------\n\n");

printf("_________________________________________________________________________________\n");
printf("|                                                                                |\n");
printf("| (1) | O Ficheiro Será Recriado (Perde Todos Os Dados Atuais)!                  |\n");
printf("| (2) | Cancelar, Quero Retornar Ao Menu!                                        |\n");
printf("|________________________________________________________________________________|\n\n");

novaopcao();

printf("\n\n----------------------------------------------------------------------------\n\n");

if(opcao == 1)
{
    system("cls");

    nfile_1 = fopen("mensagem.txt", "w");

    printf("Digite o Texto Para Conversão:\n\n");

    fseek(stdin,0,SEEK_END);

    gets(tfile); //  ----------------------The ERROR----------------------------------

    printf("%s\n\n", tfile);

    fprintf(nfile_1 ,"%s", tfile);

    fclose(nfile_1);

    nfile_1 = fopen("mensagem.txt", "r");

    printf("\n\n----------------------------------------------------------------------------\n\n");

    printf("A mensagem atual contida no ficheiro 'mensagem.txt' será apresentada abaixo:\n\n");

    while(fgets(tfile, 2000, nfile_1) != NULL)
    {
        printf("%s\n\n", tfile);
    }

    printf("\n\n----------------------------------------------------------------------------\n\n");

    fclose(nfile_1);

    printf("Prima a tecla enter para voltar ao menu!\n\n");
}
  • Relevant: https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used?rq=1 – Hong Ooi Nov 18 '19 at 01:46
  • 2
    You shouldn't use `gets`, this function is dangerous because it doesn't check for the size of the buffers and hence you can have a buffer overflow. Use `fgetc` or `fgets` instead. – Pablo Nov 18 '19 at 01:47
  • See: [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) . Use `fgets()` or POSIX `getline()` instead. – David C. Rankin Nov 18 '19 at 06:16

0 Answers0