Why isn't the comparing between the strings work?
I know for a certain that the user strings do not have any endlines at the end of them, but still I get that the username is not accepted.
char user[24];
int userLog = -1;
FILE *usernames;
usernames = fopen("usernames.cfg", "r");
if (usernames == NULL){
perror("usernames - err");
return(-1);
}
while(fgets(user, sizeof(user), usernames) !=NULL){
strtok(user, "\n");
printf("%s -- %s\n", user, possibleUsername);
// First edition of question contained:
// if((possibleUsername, user) == 0)
// Still having problems with this version:
if(strcmp(possibleUsername, user) == 0)
userLog = 1;
else
userLog = 0;
}
if(userLog == 1) printf("Username accepted: %s\n", possibleUsername);
else if(userLog == 0) printf("Username doesn't exist in the database.\n");
fclose(usernames);
usernames.cfg:
user
justplayit
etc
if((possibleUsername, user)lacks astrcmp.usernames.cfg? If they're on a new line always they end with newlines but theuserstring doesn't as you said. That might be the problem.break;e.guserLog = 1;-->{ userLog = 1; break;}strtok. BLUEPIXY nails it - the loop continues over all names even though one matched.userLog == -1which would happen if there are no names in the database yet. You'd use anelseclause to cover any other value inuserLog, though the only plausible value is -1.