File with strange characters

Asked

Viewed 65 times

1

I have a string that receives a keyboard input value and a file that contains a string. What I need to do is compare the typed string with the file string and check if they are the same, but I went to see what kind of string the file was taking and it was coming with strange characters (play button, '@' and down arrow). The code in thesis seems to work, however I would like to know why the string I’m bringing to compare with the one typed by the user is coming with these characters.

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

main()
{

setlocale(LC_ALL,"portuguese");

FILE *arquivo;
char frase[6];
char frase2[6];
int i;

if(arquivo = fopen("teste.txt", "r") == NULL)
{
    puts("ERRO! IMPOSSÍVEL ABRIR O ARQUIVO");
}
else
{
    printf("Digite a frase: \n");
    gets(frase);

    fgets(frase2, 6, arquivo);
    puts(frase2);

    if(i = strcmp(frase, frase2) == 0)
    {
        puts("Encontrado");
    }
    else
    {
        puts("Não encontrado");
    }

}

fclose(arquivo);
getch();
return 0;
}
  • What’s in the file?

  • The file has the name "Lucas", IE, when the user type string on the keyboard, is to make a comparison. However, when I pull this string out of the file to see what’s going on, some strange characters come out. Then the program always says "Not Found". I already checked and the file is with ANSI text and still did not help.

1 answer

2


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

main(){

    setlocale(LC_ALL,"portuguese");

    FILE *arquivo;
    char frase[6];
    char frase2[6];
    int i;

    if(arquivo = fopen("teste.txt", "r") == NULL)//Uma estrutura 
    //condicional só aceita valores boleanos e você está atribuindo
    //então coloque assim

    //arquivo = fopen("teste.txt", "r");

    //if(arquivo == NULL)
   {
        puts("ERRO! IMPOSSÍVEL ABRIR O ARQUIVO");
   }else{
        printf("Digite a frase: \n");
        gets(frase);//não é recomendável usar gets() pois não terá uma
        //controle sobre o tamanho da string que o usuário digitar

        fgets(frase2, 6, arquivo);
        puts(frase2);

        if(i = strcmp(frase, frase2) == 0)//como disse antes sem atribuição em 
        //estruturas condicionais, tire o 'i' e como ele não irá servir para nada 
        //no código apague ele lá no ínicio

        //if(strcmp(frase, frase2)){
            puts("Encontrado");
        }else{
            puts("Não encontrado");
        }

    }

    fclose(arquivo);
    getch();//getchar();

    return 0;
}

I think there are no more mistakes.

  • Thank you so much! This one gave me a headache!

Browser other questions tagged

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