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("s", u1.fname);
printf("\nLAST NAME...");
scanf("s", u1.lname);
printf("\nFATHER's NAME...");
scanf("s", u1.fathname);
printf("\nMOTHER's NAME...");
scanf("s", u1.mothname);
printf("\nADDRESS..");
scanf("s", 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("s", u1.pnumber);
printf("\nAGE...");
scanf("%d", &u1.age);
printf("\nUSERNAME.. ");
scanf("s", u1.username);
printf("\nPASSWORD..");
scanf("s", 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("s", username);
gotoxy(35, 14);
printf("ENTER PASSWORD.. ");
scanf("s", 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);
}
CodePudding user response:
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("s", p1.password);
to scanf("s", u1.password);
.