2

I am trying to read and compare some data from a text file. The text file looks like the following where the white space is different between each line:

Username    Password
Maolin     111111
Jason    222222
Mike        333333

I have the following code which works if the white space is only a single " " space.

bool authenticate_login(char *username, char *password)
{

    FILE *file;
    char *file_username;
    char *file_password;
    bool match;

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

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


    while (fscanf(file, "%s %s\n", file_username, file_password) > 0) {

        if (strcmp(file_username, username) == 0 && strcmp(file_password, password) == 0) {
            match = true;
            break;
        }
    }
    printf("\nMade it here babe %d", match);

    fclose(file);
    return match;
}

Can someone please show me how to change my code so that I can ignore the white space regardless of how much there is?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Jane Doe
  • 187
  • 1
  • 12
  • 1
    Hint, what is the initial value of `match, file_username, file_password`? It is not a white-space problem. – chux - Reinstate Monica Sep 23 '18 at 03:07
  • 1
    first assign value to `match` and also see [this](https://stackoverflow.com/questions/20712572/how-to-ignore-whitespaces-in-fscanf) And `file_username` & `file_password` are char pointers, first you need to allocate memory for them then only you can use in `fscanf()` or take as char array like `char file_password[50]`. – Achal Sep 23 '18 at 03:12

0 Answers0