0

I am trying to create a program to enter into a system with username & password. But after creating the account, while in signin when I entered my username & password (stored from SignUP function), it shows incorrect. Where did I made the mistake and how can I fix it? I have given my specific code related to this problem. TIA

struct information
{
char username[20];
char password[20];
int date, month, year;
char pnumber[20];
int age[20];
char fname[20];
char lname[20];
char fathname[20];
char mothname[20];
char address[50];

};  

void signup()
{
char username[20];
char password[20];
int passwordlength, i, seek = 0;
char ch;
FILE *fp, *fu;
struct information u1;
struct information p1;


// Opening file to
// write data of a user
fp = fopen("username.txt", "ab");

system("cls");
printf("\n\n!!!!!CREATE YOUR ACCOUNT!!!!!");

printf("\n\nFIRST NAME...");
scanf("%19s", u1.fname);

printf("\nLAST NAME...");
scanf("%19s", u1.lname);

printf("\nFATHER's NAME...");
scanf("%19s", u1.fathname);

printf("\nMOTHER's NAME...");
scanf("%19s", u1.mothname);

printf("\nADDRESS..");
scanf("%19s", u1.address);

printf("\nDATE OF BIRTH...");
printf("\nDATE-");
scanf("%d", &u1.date);
printf("\nMONTH-");
scanf("%d", &u1.month);
printf("\nYEAR-");
scanf("%d", &u1.year);

printf("\nPHONE NUMBER...");
scanf("%19s", u1.pnumber);

printf("\nAGE...");
scanf("%d", &u1.age);

printf("\nUSERNAME.. ");
scanf("%19s", u1.username);

printf("\nPASSWORD..");
scanf("%19s", p1.password);


fwrite(&u1, sizeof(u1), 1, fp);

fclose(fp);
printf("\n\nACCOUNT CREATED SUCCESSFULLY.\n");

char option[10];
printf("\nPRESS ANY KEY THEN ENTER TO GO TO SIGN IN PAGE");
scanf("%s", option);
signin();
}

void signin()
{
system("cls");

char username[20];
char password[20];

int i, j, k;
char ch;
FILE *fp, *fu;
struct information u1;
struct information p1;


// Opening file of
// user data
fp = fopen("username.txt","rb");

if (fp == NULL)
{
    printf("\nERROR IN OPENING FILE\n");
    printf("FILE DOESN'T EXIST\nYOU HAVE TO CREATE AN ACCOUNT FIRST\n");
    printf("PRESS ANY KEY & ENTER TO CREATE AN ACCOUNT\n");
    char option[10];
    scanf("%s", option);
    signup();
}
gotoxy(35, 10);
printf("==== LOG IN ====");

// Take input
gotoxy(35, 12);
printf("ENTER USERNAME.. ");
scanf("%19s", username);

gotoxy(35, 14);
printf("ENTER PASSWORD.. ");
scanf("%19s", password);


// Checking if username & password
// exists in the file or not
while (fread(&u1, sizeof(u1), 1, fp))
{
    if (strcmp(username, u1.username) == 0)
        if (strcmp(password, u1.password) == 0)
        {
            mainmenu();
        }

fclose(fp);
}
root1_2
  • 7
  • 4
  • 3
    Please eliminate those parts of the code not related to your question. – Scott Hunter Oct 25 '21 at 19:51
  • 2
    Please clean up your code to make it a [mre]. For example remove all the commented out code and remove all unnecessary code such as the menuing. You'll more likely get an answer if you put in that effort and perhaps more importantly, reducing to minimal code is also a good debugging technique that may even lead you to find the problem yourself. – kaylum Oct 25 '21 at 19:51
  • Your code produces many warnings and doesn't even compile. Besides being grossly offensive for screaming in all capitals. – Cheatah Oct 25 '21 at 19:58
  • 1
    @Barmar: How is that a duplicate? – Eric Postpischil Oct 25 '21 at 19:58
  • You should indent your code properly, for example like the samples in your C text book. Even for experts it's hard to work with poorly formatted code, let alone beginners. – Jabberwocky Oct 25 '21 at 20:24
  • @EricPostpischil He's printing "not found" every time through the loop, rather than waiting until he's read the entire file. But he's since edited the question to remove that. – Barmar Oct 25 '21 at 20:38

1 Answers1

0

In the signup() function you are reading the password into the p1.password field, but then only writing the u1 struct to the file. Simply change scanf("%19s", p1.password); to scanf("%19s", u1.password);.

gwell
  • 2,695
  • 20
  • 20
  • Thank you so much. I can't believe I missed that tiny little details. I was working on it for literally hours. – root1_2 Oct 25 '21 at 21:10