dimanche 19 avril 2015

C - Login using file handling and linked list

I am creating an account using Create New Account option, & then writing the created username pwd to the file, but when I rerun the code & try to login using the same id, it fails. Using string compare, but it seems string is not being read.

struct user{ char username[10]; char password[10]; struct user *next; }*sUser,*pUser;



userlogin(void){
FILE *fp;
char uName[10], pwd[10];int i;char c;
sUser=pUser=(struct user *)malloc(sizeof(struct user));

printf("1. Login Through An Existing Account\n2. Create New account\n");
scanf("%d",& i);
system("cls");
switch(i){
case 1:
fp=fopen("user.dat", "w");
printf("Username: ");
scanf("%s",&uName);
printf("Password: ");
scanf("%s",&pwd);
fread (pUser, sizeof(struct user), 1, fp);
while(pUser!=NULL){
if(pUser->username==uName){
if(pUser->password==pwd){
accessUser();
}
}
pUser=pUser->next;
}
break;


case 2: do{ fp=fopen("user.dat", "r");
printf("Choose A Username: ");
scanf("%s",&pUser->username);
printf("Choose A Password: ");
scanf("%s",&pUser->password);
printf("Add another account? (Y/N): ");
fflush(stdin);
scanf("%c",&c);
if(c=='Y'||c=='y'){
pUser->next=(struct user*)malloc(sizeof(struct user));
}
}while(c=='Y'||c=='y');
pUser->next==NULL;
fwrite (sUser, sizeof(struct user), 1, fp);
break;
}
fclose(fp);
}


EDIT:

The value of pUser->username is not what it should be Can anyone tell me what I'm doing wrong.


Aucun commentaire:

Enregistrer un commentaire