Comparing user typed variable with file variable

Asked

Viewed 1,763 times

5

Inside of my file arquivo.txt I keep two strings "nome" and "senha", They lay one on the other.

I need to take the name and password entered by the user and compare it with the name and password I have stored in the.txt file

NOTE: The information below the EOF, I already tried to do != NULL, but it was in an infinite loop.

I made a while, why enquanto for diferente de EOF, he rotates around... As I couldn’t find a way to compare the variables typed with those in the file, I called the strcpy. I created two char variables and copied the nome and the senha that were typed by the user to string2 and string3. So that then I can compare the nome with the string2 and the senha with the string3.

But there’s always a warning I can’t understand.

warning: format '%s' expects argument of type 'char' *', but argument 3 has type 'char'(*)[100]' 

I understand what is written, but I don’t understand what it interferes with the code.

The error that gives in the program is the following: Everything I type, it enters as true, enters the if and prints Welcome!

FILE *fp;
    fp = fopen("arquivo.txt", "r");

    if (fp == NULL) {

        printf("\nO arquivo não foi aberto!");
        exit(0);

    }

    char nome[100], senha[100], string2[100],string3[100]; 

    printf("\n\tNOME........: ");
    scanf("%s", nome);
    //Tentei fazer com fgets, mas da erro quando uso no while, então
    //resolvi deixar o scanf mesmo

    printf("\n\tSENHA........: ");
    scanf("%s", senha);

    printf("\n");

// printf("\n%s %s", nome, senha); //testar o que foi digitado pelo usuario

    printf("\n");

    while( (fscanf(fp, "%s %s", &nome, &senha)) != EOF ) {

        strcpy(string2, nome);
        strcpy(string3, senha);

        if ( (strcmp(nome, string2) == 0) && (strcmp(senha, string3) == 0) ) {

            printf("\nBem-Vindo!\n");


        } else {

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

        }

    }

    }

    fclose(fp);

}
  • 1

    In his fscanf you are storing what is in the file inside nome and senha and not within string2 and string3.

  • Wow, Daviaragao ... Thank you very much. I was stuck in this mistake a long time ago and I didn’t understand why. I looked in several places and couldn’t find... It worked super well here. Thank you very much. ;)

2 answers

4


The condition of your while should be

//while( (fscanf(fp, "%s %s", &nome, &senha)) != EOF ) {
while ((fscanf(fp, "%s%s", nome, senha)) == 2) {

The differences are

  • use of & in the variables
    use the & specifies address of the array (type char(*)[100])
    without the & type is automatically converted to char*

  • instead of testing if the result is different from EOF, the test to be equal to 2 covers more situations that may be error.

  • the space between the two %s is unnecessary

  • formatting to my liking :-)

  • Oh yes, now I understood why to give that warning... I was indicating the address of the variable and not its value. I just followed what Daviaragon taught me up there, and it worked... So I can do it both his way and his way that in the end, the result is the same?

  • As @Daviaragao noticed, you must user string2 and string3 instead of nome and senha and stop copying. fscanf(fp, "%s%s", string2, string3)

  • Ah yes, ok... Thank you. ;)

4

The @pmg response eliminates the Warning when compiling. But there is a logic error in your code. When reading the files in the file you are overwriting the values that the user reported as usuario and senha. Store what’s in the file inside string2 and string3.

  • I didn’t understand it very well. I did what you told me up there and it is running in the right way. At least in my view (I’m a beginner). rs When you say "Store what’s in the file inside string2 and string3", do you mean to me on the line after while copy the values of the name and password in string2 and string3? I should use strcpy for that too?

Browser other questions tagged

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