User Authentication (Login Check Function)
This C program will check the User ID and Password entered by user with the UserID and Password stored into login.txt file. Each line of login.txt contains UserID Password of one user.
Sample login.txt file contains data of 5 users which are as under:
101 abc123
102 guest
103 hi
guest guest
admin admin
Make sure that each user ID and Password is separated by space.
int Login_check(char id[], char pass[]);
int main()
{
int check=0;
char id[20], pass[20];
printf("Enter your User ID:");
scanf("%s", id);
printf("Enter your Password:");
scanf("%s", pass);
check = Login_check(id, pass);
if (check==1)
printf("\nLogin successful...\n\nDisplay Menu Here...\n");
//write main program logic here...
else
printf("\nLogin failed. Try again...\n");
return 0;
}
int Login_check(char id[], char pass[])
{
FILE *fp;
char *fid, *fpass, *token;
int check=0;
const char str[40] = "", s[2] = " ";
fp = fopen("login.txt", "r");
if(fp==NULL)
{
printf("File error..."); exit(0);
}
while(fgets(str, 40, fp)!=NULL)
{
fid = strtok(str,s);
fpass = strtok(NULL,s);
fpass[strlen(fpass)-1]='\0';
if((strcmp(id,fid) == 0) && (strcmp(pass,fpass) == 0 ))
{
check=1;
}
}
return check;
}
Output of Program
Enter your User ID:guest
Enter your Password:guest
Login successful...
Display Menu Here...
No comments:
Post a Comment