Reading and Comparing file login and password in C

2

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?

    
asked by anonymous 11.11.2015 / 18:16

1 answer

1

You said the line, but I do not know what the 146 line is (it has 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 int . This line should be rewritten by replacing EOF with NULL , because when there is no more data to be read from the file, this command returns NULL . Otherwise, it returns the same passed in the first parameter (in this case, str ).

Follow 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 with both login and senha .

    
11.11.2015 / 23:58