5
Inside of my file arquivo.txt
I keep two strings "nome"
and "senha"
, They lay one on the other.
I need to take the name and password entered by the user and compare it with the name and password I have stored in the.txt file
NOTE: The information below the EOF, I already tried to do != NULL, but it was in an infinite loop.
I made a while
, why enquanto for diferente de EOF
, he rotates around...
As I couldn’t find a way to compare the variables typed with those in the file, I called the strcpy
.
I created two char variables and copied the nome
and the senha
that were typed by the user to string2
and string3
. So that then I can compare the nome
with the string2
and the senha
with the string3
.
But there’s always a warning I can’t understand.
warning: format '%s' expects argument of type 'char' *', but argument 3 has type 'char'(*)[100]'
I understand what is written, but I don’t understand what it interferes with the code.
The error that gives in the program is the following: Everything I type, it enters as true, enters the if and prints Welcome!
FILE *fp;
fp = fopen("arquivo.txt", "r");
if (fp == NULL) {
printf("\nO arquivo não foi aberto!");
exit(0);
}
char nome[100], senha[100], string2[100],string3[100];
printf("\n\tNOME........: ");
scanf("%s", nome);
//Tentei fazer com fgets, mas da erro quando uso no while, então
//resolvi deixar o scanf mesmo
printf("\n\tSENHA........: ");
scanf("%s", senha);
printf("\n");
// printf("\n%s %s", nome, senha); //testar o que foi digitado pelo usuario
printf("\n");
while( (fscanf(fp, "%s %s", &nome, &senha)) != EOF ) {
strcpy(string2, nome);
strcpy(string3, senha);
if ( (strcmp(nome, string2) == 0) && (strcmp(senha, string3) == 0) ) {
printf("\nBem-Vindo!\n");
} else {
printf("\nSeu login ou senha estão errados!");
}
}
}
fclose(fp);
}
In his
fscanf
you are storing what is in the file insidenome
andsenha
and not withinstring2
andstring3
.– DaviAragao
Wow, Daviaragao ... Thank you very much. I was stuck in this mistake a long time ago and I didn’t understand why. I looked in several places and couldn’t find... It worked super well here. Thank you very much. ;)
– Marcielli Oliveira