Hi,
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,"\0"); work?
A snippet of the code is as follows:
do{
p = fgets(temp,90,fp); //use fgets to read lines from the file
puts(temp);
if(p != NULL)
{
if(strcmp(userinput,temp) == 0) //doesnt work!
{
match = 1; //matching flag
printf("Login Authorised\n");
break;
}
}
}while(p != NULL);
if(match == 0)
printf("Unauthorised Login\n");
Advice would be appreciated! :D