Question 2 (50 points): User Authentication Create a text file UserPassword.txt
ID: 3587745 • Letter: Q
Question
Question 2 (50 points): User Authentication Create a text file UserPassword.txt with the following two lines: Alice P4ssword Write a program to complete the following tasks: . Upon execution, the program will prompt the user to type in 2. The program will compare with the typed in name with the 3. If the name does not match, the program will prompt user to his/her name. name stored in UserPassword.txt (the first line of the file). reenter the name; if the name match, the program will proceed to ask for the password. 4. Give the user 3 times to enter password, and compare the typed in value with the password stored in UserPassword.txt (the second line of the file). If the password is correct, display a greeting message and terminate the program; otherwise, display an error message and terminate the program. Following is an sample screen of the program in execution: Welcome! Please enter User Name: Bob User Name does not exist! Welcome! Please enter User Name: Alice Welcome back, Alice! Please enter User Password: 123 Password for Alice entered incorrectly! Please enter User Password: Congratulations! You have successfully logged in. (Or: You have entered incorrect password for 3 times, system is shutting down...) Your file will have the following documentation comments before the class header:Explanation / Answer
This C program will fullfil all your requirements.Please go threw it.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Function for user authentication
void UserAuth(void);
void main(void)
{
UserAuth();
}
void UserAuth(void)
{
FILE * fp; //File pointer
int counter = 0; //counter for password
char user[32],*buffer; // user and extra buffer
char passwd[32]; // password
int size =32; // total size of username and password is 32
buffer = (char *) malloc (1*size);
lable_usr:
printf("Welcom! Please Enter User Name: ");
scanf(" %s",user); //scan user name
fp = fopen("UserPassword.txt","r"); //open data file
if(fp == NULL)
{
perror("fopen"); // if file not exist print error
return ;
}
getline(&buffer,(size_t *)&size,fp); // take first line
if(strncmp(buffer,user,strlen(user))) //compare with user entered name
{
printf("User Name does not exist! "); // if user not match print error
rewind(fp); // rewind file pointer to first latter
goto lable_usr; // goto lable of enter user
}
printf("Welcom back, %s! ",user);
memset(buffer,0,size); // clean buffer
getline(&buffer,(size_t *)&size,fp); // get second line
lable_pw:
printf("Please Enter User Password: ");
scanf(" %s",passwd); // scan password
if(strncmp(buffer,passwd,strlen(passwd))) // check the password of data file and user entered password
{
printf("Password for %s entered Incorrectly! ",user); // print if password is not currect
counter++; // incress counter
if(counter >= 3) // chech wether count is maximum or not
{
printf("You have entered incorrect password for 3 times, system is shutting down.. "); // if count exsosted comeout
fclose(fp); // close file
return ; // return back and close program
}
goto lable_pw; // if count not exsosted go to password lable.
}
printf("Congratulations! You have successfully logged in. "); // if password currect welcom user.
fclose(fp); // close file
}
create UserPassword.txt in same directory.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.