0

This is a practice code for a login system.

struct login
{
    char username[30];
    char password[20];
};
void login (void);
void registration (void);
int main (void)
{
    int option;
    printf("Press '1' to Register\nPress '2' to Login\n\n");
    scanf("%d",&option);
    getchar();
    if(option == 1)
        {
            registration();
        }
    else if(option == 2)
        {
            login();
        }
}
void login (void)
{
    char username[30],password[20];
    FILE *log;
    log = fopen("login.txt","r");
    if (log == NULL)
    {
        fputs("Error at opening File!", stderr);
        exit(1);
    }
    struct login l;
    printf("\nPlease Enter your login credentials below\n\n");
    printf("Username:  ");
    printf("\n");
    fgets(username, 30, stdin);
    printf("\nPassword: ");
    printf("\n");
    fgets(password, 20, stdin);
    while(fread(&l,sizeof(l),1,log))
        {
        if(strcmp(username,l.username)==0 && strcmp(password,l.password)==0)
            {   
                printf("\nSuccessful Login\n");
            }
        else 
            {
                printf("\nIncorrect Login Details\nPlease enter the correct credentials\n");
            }
        }
    fclose(log);
    return;
}
void registration(void)
{
    char firstname[15];
    FILE *log;
    log=fopen("login.txt","w");
    if (log == NULL)
    {
        fputs("Error at opening File!", stderr);
        exit(1);
    }
    struct login l;
    printf("Welcome to UrBook. Please choose a username and password as credentials for system login.\nEnsure the username is no more than 30 characters long.\nEnsure your password is at least 8 characters long and contains lowercase, uppercase, numerical and special character values.\n"); 
    printf("\nEnter Username:\n");
    scanf("%s",l.username);
    printf("\nEnter Password:\n");
    scanf("%s",l.password);
    fwrite(&l,sizeof(l),1,log);
    fclose(log);
    printf("\nRegistration Successful!\n");
    printf("Press any key to continue...");
        getchar();
    login();
}

I can't seem to figure out the problem in the code. It always shows incorrect login details no matter what I put in. As an example, I wrote login as abc, password as 123. And similarly with some different inputs, but the same error was being shown. I also tried to use fscanf instead of fgetf, but it didn't work.

  • 1
    Well it must be that one or the other of the strcmp is returning non-zero when you expect it to return zero. Why don't you look at the exact values of `username`, `l.username` `password` and `l.password` at that point in the execution and see what's happing there (either using a debugger, or inserting printfs). Once you discover which of these values is not what you expect, perhaps you can investigate from there. – Paul Hankin Dec 16 '21 at 14:24
  • Please see [fgets() doesn't work after scanf](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf). Best to stick to a single input method. – Weather Vane Dec 16 '21 at 14:36
  • Could you elaborate a bit @Gerhardh? Do you mean the \n? – DarthMalak2003 Dec 16 '21 at 14:36
  • 1
    Yes, `fgets` will keep the `\n` in the buffer if the password is not too long. That is not the case when you use `scanf`. If you mix both methods, `strcmp` will not see identical strings. – Gerhardh Dec 16 '21 at 14:38

0 Answers0