ccomparisonfgetsstrtokstrcmp

In C, strcmp() is not returning 0 when it should


I am comparing user input of usernames and passwords. the string that are being compared to are being read in from a file. For whatever reason, it is appropriately comparing the usernames, but not the passwords.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int MAX_SIZE = 100;

int main()
{
    FILE *fp;
    char *filename = "userdata.txt";
    char arr[100][MAX_SIZE];
    
    //open for writing
    fp = fopen(filename, "r");
    
    //verify open
    if(fp == NULL)
    {
        printf("%s does not exist", filename);
        return 0;
    }

    int index = 0;

    //read file into array
    while(fgets(arr[index], MAX_SIZE, fp) != NULL) 
    {
        index++;
    }
    
    //username input
    char username[100];
    printf("Username: ");
    scanf("%s", username);

    //password input
    char password[100];
    printf("Password: ");
    scanf("%s", password);
    
    
    int check1 = 0;
    int check2 = 0;
    int x;
    for (int i = 0 ; i<index ; i++)
    {
        char *token = strtok(arr[i], " ");
       
        while (token != NULL)
        {
            x = strcmp(token,username);
            printf("%d\n",x);
            printf("%s %s\n",token,username);
            if(!strcmp(token,username))
            {
                check1 = 1;
            }
            
            token = strtok(NULL, " ");
            x = strcmp(token,username);
            printf("%d\n",x);
            printf("%s %s\n",token,password);
            if(!strcmp(token,username))
            {
                check2 = 1;
            }
            
            token = strtok(NULL, " ");
            
            if(check1&&check2)
            {
                printf("The amount is: %s\n",token);
                return 0;
            }
    
            token = strtok(NULL, " ");
            
            check1=0;
            check2=0;
        }
    }
    printf("Username/Password mismatch!!!\n");
    return 0;
}

console output:

Username: user1
Password: password1
0
user1 user1
-5
password1 password1
1
user2 user1
-5
password2 password1
2
user3 user1
-5
password3 password1
3
user4 user1
-5
password4 password1
4
user5 user1
-5
password5 password1
5
user6 user1
-5
password6 password1
Username/Password mismatch!!!

Solution

  • When fgets reads in a line of text, it also reads and stores the newline at the end of the line.

    This means that when you split the string with strtok and use " " as the delimiter, the read-in password includes the newline, while the password from the user read via scanf with the %s format specifier does not, causing the mismatch.

    You can fix this by including the newline character in the set of delimiters given to strtok.

    char *token = strtok(arr[i], " \n");
    ...
    token = strtok(NULL, " \n");
    

    Also, your second set of calls to strcmp is checking the username instead of the password. So instead of this:

            x = strcmp(token,username);
            printf("%d\n",x);
            printf("%s %s\n",token,password);
            if(!strcmp(token,username))
    

    You want this:

            x = strcmp(token,password);
            printf("%d\n",x);
            printf("%s %s\n",token,password);
            if(!strcmp(token,password))