Reading and Comparing C File Login and Password

Asked

Viewed 3,661 times

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?

  • 1

    What error is happening?

  • 1

    You want to record the login and password in the file that?

  • 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.

  • The ta no while error of the tela_login screen();

  • on line 146, error: comparison between Pointer and integer

1 answer

1


You said the line, but I don’t know which line is 146 (there’s no line numbering...).

Reading your code, I believe the problem is on the line:

while(fgets(str, tamanho, stdin) != EOF) {

The command fgets returns a char*, not a int. This line must be rewritten by replacing EOF for NULL, because when there is no more data to read from the file, this command returns NULL. Otherwise, it returns the same passed in the first parameter (in the case, str).

Follows the code:

while(fgets(str, tamanho, stdin) != NULL) {

Or simply:

while(fgets(str, tamanho, stdin)) {

I would just like to note that the line after this command does not make sense, since you compare str so much with login as to senha.

  • 1

    Oh vdd. I’m sorry, I didn’t notice the line issue. But this one where you even pointed out that was the mistake. Thank you very much, I suspected but I was looking for a way to solve the problem and I couldn’t find anything I could understand. Your explanation helped me understand the logic there as well. Oh yes, this line I put more because nothing was going right so I decided to test everything I thought I could put. But thank you. Helped me a lot. ;)

Browser other questions tagged

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