2
I am programming my system login screen with the help of files, ie save/read the data within a arquivo.txt
.
So far I was able to register the login and password of the user, but I am dealing with several errors when reading the text inside the .txt
, "take" the login and password previously entered in the register and compare with the login and password that the user entered at the time when he tried to login.
Code below is starting the writing part on login.txt
, which is the name of my file:
FILE * fp; //Está em cima do main
int main () {
fp = fopen("login.txt", "a+");
if (fp == NULL)
{
printf("\nErro na criação da abertura do arquivo!");
} else {
/*Aqui consta o menu, para levar o usuário a outras telas, caso ele queira "Cadastrar" ou "Logar" no sistema.*/
}
fclose(fp);
}
Below the registration screen:
void tela_cadastro(void)
{
printf("\n\tLOGIN........: ");
fgets(pessoas[i].login, 100, stdin);
__fpurge(stdin);
printf("\n\tSENHA........: ");
fgets(pessoas[i].senha, 100, stdin);
__fpurge(stdin);
//fprintf(fp, "\n************LOGIN************SENHA************\n");
fprintf(fp, "%s", pessoas[i].login);
fprintf(fp, "%s", pessoas[i].senha);
// printf("\n\tLogin: %s", pessoas[i].login); // teste só pra ver se ta mostrando login
//printf("\n\tSenha: %s", pessoas[i].senha);//teste só pra ver se ta mostrando senha
}
NOTE: On the screen above the registration, I can register on login.txt
all logins and passwords.
Below the login screen code, which is where I’m having trouble:
void tela_login()
{
FILE *fp;
fp = fopen("login.txt", "r"); //"r" para ler o que tem no arquivo
if (fp == NULL) {
printf("\nO arquivo não foi aberto!");
exit(0);
}
char login[100], senha[100], str[100]; //str = linha
int tamanho=100;
printf("\n\tLOGIN........: ");
fgets(login, 100, stdin);
__fpurge(stdin);
printf("\n\tSENHA........: ");
fgets(senha, 100, stdin);
__fpurge(stdin);
// printf("\nLogin: %s", login); teste p ver o que estava imprimindo
// printf("\nSenha: %s", senha);teste p ver o que estava imprimindo
while(fgets(str, tamanho, stdin) != EOF) {
if ( (strcmp(str, login) == 0) && (strcmp(str, senha) == 0) ) {
printf("\nTESTE");
} else {
printf("\nERRO");
}
}//Fim while
fclose(fp);
}
If someone can show me the direction. Like, what should I research to solve this problem. I’ve tried everything, but I don’t think I can understand how the function works, but I’ve researched and I still need a more specific explanation. Can someone help me?
What error is happening?
– Pablo Almeida
You want to record the login and password in the file that?
– gato
Save I already saved. The error is in the login. I don’t understand how to compare a login.txt value with the value entered by the user in the login.
– Marcielli Oliveira
The ta no while error of the tela_login screen();
– Marcielli Oliveira
on line 146, error: comparison between Pointer and integer
– Marcielli Oliveira