I'm programming my system login screen with the help of files, ie saving / reading the data within a arquivo.txt
.
So far I have been able to register the login and password of the user, however I am dealing with several errors when reading the text that is inside .txt
, "get" the login and the password entered previously in the registry and compare to the login and password that the user entered when they tried to log in.
Code below is starting the writing part in 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: In the above screen, I can register all logins and passwords in login.txt
.
Below the login screen code, which is where I'm having problem:
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 look for to solve this problem. I have tried everything, but I believe that I can not understand how the function works, but I have already researched and still need a more specific explanation. Can anyone help me?