User and password validation in C

Asked

Viewed 1,397 times

0

I am making a game that requires authentication to play. How do I validate user and password saved in a file .txt?

The code I’ve developed so far is this:

void login(){
int escolhe_dificuldade(); // função para selecionar dificuldade do jogo

FILE *pont_arq;
     pont_arq = fopen("arquivo_palavra.txt", "r");// arquivo onde esta armazenado nome,senha de acesso por linhas.
     if(pont_arq == NULL) {
     printf("Erro na abertura do arquivo!");
     return 1;
}

char usuario[20]; 
char senha[20],string2[100],string3[100];



printf(" Digite o usuario: ");
    scanf("%s", usuario);
    printf(" Digite a senha: ");
    scanf("%s", senha);
     printf("\n");
     printf("\n");

     while( (fscanf(pont_arq, "%s %s", &string2, &string3)) != EOF ) {

        //strcpy(string2, usuario);
        //strcpy(string3, senha);

        if ( (strcmp("%s" == string2 && "%s" == string3)) ) {

            printf("\nBem-Vindo!\n");
            playCPU(escolhe_dificuldade());
        } else {

            printf("\nSeu login ou senha estão errados!");

        }

    }


    fclose(pont_arq);    


  }
  • Study the syntax of the strcmp function, for example: http://pubs.opengroup.org/onlinepubs/9699919799/functions/strcmp.html

1 answer

1


The first step to not having any flaws in your authentication is to define what your file will look like where the login and password will be stored. One line the user and the next the password?

correct scanf:

scanf("%s", &usuario);
scanf("%s", &senha);

Try this code:

if ( ( strcmp( string2, usuario) == 0) &&
     ( strcmp( string3, senha ) == 0 ))
{
    // OK
...
}

Browser other questions tagged

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