0

I am trying to write a simple username/password authentication system. Currently my username works and compares the strings correctly but what i have found is that my variable 'password' is being changed. Lets say the username is 'James' the password '123456', the username works perfectly and compares correctly but the password is passed to the method correctly but upon the line while (fscanf(file, "%s %s\n", file_username, file_password) > 0) the 'password' variable is suddenly changed to 'James' and this is then compared against the file variable file_password.

bool authenticate_login(char *username, char *password)
{
    printf("\nMade it to authentication_login\n");

    FILE *file;
    char *file_username;
    char *file_password;
    bool match;
    printf("\n%s\n", username);
    printf("\nThe password is coming in as: %s\n", password);

    if ((file = fopen("Authentication.txt", "r")) == NULL) {
        perror("fopen");
        return false;
    }

    while (fgetc(file) != '\n'); /* Move past the column titles in the file. */

    /* Each line in the file contains the username and password separated by a white space. */
    while (fscanf(file, "%s %s\n", file_username, file_password) > 0) {
        printf("\n TESTING THIS LINE: %s", password);

        /* Check if the username matches one in the file, and if the password matches for that username. */
        printf("\n\n%s",file_username);
        printf("\n%d",strcmp(file_username, username));
        printf("\n\n%s",file_password);
        printf("\n%s", password);
        printf("\n%d",strcmp(file_password, password));

        if (strcmp(file_username, username) == 0 && strcmp(file_password, password) == 0) {
            match = true;
            break;
        }
    }

    fclose(file);
    return match;
}
Jane Doe
  • 187
  • 1
  • 12

1 Answers1

1

You shouldn't use pointers for file_username and file_password, you should use char arrays.

#define MAX_USERNAME 20
#define MAX_PASSWORD 80

char file_username[MAX_USERNAME];
char file_password[MAX_PASSWORD];
SHR
  • 7,940
  • 9
  • 38
  • 57