txt file includes: Please explain while writing code ! The file \"user.pswd.txt\
ID: 3891564 • Letter: T
Question
txt file includes:
Please explain while writing code !
The file "user.pswd.txt" is a comma-delimited file. Each field in the file is separated from the next field by a comma. The five fields are lastname, firstname, number of KBytes of data occupied by the user's files, username, password Note that this file was created with Notepad.exe, i.e. it has no formatting symbols in it such as those found in Microsoft Word files, etc.. So, as you are aware, if "user.pswd.txt " contains 100 characters (letters, numbers, blanks, symbols) and 3 newlines, then the file contains exactly 106 bytes (two bytes each for the newlines) Write a program to 1) indicate to the program-user that they are "attempting to login to a protected system". 2) prompt the program-user for username and password 3) check the password file to see whether the username and password are valid. The password file "user.pswd.txt" will be located in the folder from which your program is run 4) if the username or password entered by the program-user does not indicate a valid user, indicate to the user that the "username or password is invalid", and re-prompt the program-user for a usermame and password. If the program-user enters 3 incorrect attempts to login, close your program 5) If the program-user enters a valid username and password, greet the user by their first name and indicate to them the number of KBytes of storage that they are currently usingExplanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct user_record{
char lastname[15];
char firstname[15];
long storage;
char username[15];
char password[15];
};
int readFile(char *filename, struct user_record users[]);
int findUser(struct user_record users[], int size, char *username, char *password);
char* trimSpaces(char *s);
int main(){
struct user_record users[20];
int size;
char username[15], password[15];
int attempts = 0;
int index = -1;
char filename[20] = "user.pswd.txt";
size = readFile(filename, users);
printf("You are attempting to login to a protected system! ");
while(attempts < 3 && index == -1){
printf(" Enter username: ");
scanf("%s", username);
printf("Enter password: ");
scanf("%s", password);
index = findUser(users, size, username, password);
if(index == -1){
printf("Username or password is invalid! Please try again. ");
attempts++;
}
}
if(index == -1)
printf("Unable to login as you did not provide correct credentials in 3 attempts! ");
else
printf("Welcome %s! Your current storage usage is %ld KBytes ", users[index].firstname, users[index].storage);
}
int readFile(char *filename, struct user_record users[]){
char line[256];
char *token;
FILE *fp = fopen(filename, "r");
int n = 0;
if(fp == NULL){
printf("ERROR: could not read file %s ", filename);
exit(1);
}
fgets(line, 256, fp);
while(!feof(fp)){
//printf("%s ",line);
token = strtok(line, ", ");
strcpy(users[n].lastname, trimSpaces(token));
token = strtok(NULL, ", ");
strcpy(users[n].firstname, trimSpaces(token) ); //skip over the space character and copy
token = strtok(NULL, ", ");
users[n].storage = atol(token);
token = strtok(NULL, ", ");
strcpy(users[n].username, trimSpaces(token) ); //skip over the space character and copy
token = strtok(NULL, ", ");
strcpy(users[n].password, trimSpaces(token) ); //skip over the space character and copy
fgets(line, 256, fp);
//printf("|%s|%s| ",users[n].username, users[n].password);
n++;
}
fclose(fp);
return n;
}
int findUser(struct user_record users[], int size, char *username, char *password){
int i;
for(i = 0 ;i < size; i++){
if(strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0)
return i;
}
return -1; //not found matching details
}
//remove leading and trailing white spaces
char* trimSpaces(char *s){
int i = 0, j = strlen(s) - 1;
int k;
while(s[i] == ' ' || s[i] == ' ' || s[i] == ' ')
i++;
while(s[j] == ' ' || s[j] == ' ' || s[j] == ' ')
j--;
for(k = 0; i <= j; i++, k++)
s[k] = s[i];
s[k] = '';
return s;
}
output
-----
You are attempting to login to a protected system!
Enter username: abc
Enter password: test123
Username or password is invalid! Please try again.
Enter username: harley88
Enter password: crosscountry1
Welcome Erik! Your current storage usage is 337 KBytes
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.