I am writing a simple password management program using C. The program asks the user for username and password and stores them in 2 character arrays. The 2 character arrays are then concatanated using strcat. Then the password.txt file is read and they are also stored in a character array.
Then, I use strcmp to compare the char array containing the user input with the char array containing the lines read from the file. I used if(strcmp(userinput,temp) == 0) to try to detect a match but even if there is a match, strcmp cant detect it.
Could the problem be caused by the random characters near the end of the character arrays which cause the mismatch? I purposely set the array to be longer than the username so as to prevent overflow. Does inserting null using strcat(userinput,""); work?
A snippet of the code is as follows:
p = fgets(temp,90,fp); //use fgets to read lines from the file
if(p != NULL)
if(strcmp(userinput,temp) == 0) //doesnt work!
match = 1; //matching flag
}while(p != NULL);
if(match == 0)
Advice would be appreciated!