Question

In: Computer Science

Hi, I am wondering how to create code that will allow (in c) the console to...

Hi, I am wondering how to create code that will allow (in c) the console to read a password from a .txt file, and have the ability to change this password?

I have a function that logs certain users in using simple if statements and hardcoded passwords, but i would like the ability to be able to change the password and read it from a .txt so it is editable instead of having it in a function in the code.

Thanks

Solutions

Expert Solution

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
char login_pass[20];
char login_user[20];
char stored_pass[20];
char stored_user[20];

FILE *fptr;

if ((fptr = fopen("credentials.txt","r")) == NULL){
printf("Error! opening file");

// Program exits if the file doesn't exist.
return -1;
}

printf("Username: ");
scanf("%s", &login_user);


while(strcmp(login_user, stored_user)){

//if login and stored username are equal, function strcmp returns 0,
//at first loop there are not equal, as the stored_user is empty, so the condition will be true.

fscanf(fptr,"%s", &stored_user);
if(feof(fptr)){ //if it is the end of file
printf("Username does not exist.\n ");
return -1;
}
}

//now console prompts user to enter password to confirm username
printf("Password: ");
scanf("%s", &login_pass);


//since username and password are in the same line, password next to
//correctly entered username will be feteched and saved to stored_pass
fscanf(fptr,"%s", &stored_pass);


//this loop will continously check, if the entered password and stored password are same or not
int count = 3;
while(strcmp(stored_pass,login_pass)){
if(count == 1)
return -1;
else{
count--;
printf("Wrong password. Only %d attempts left. \n", count);
printf("Please, Try again:\n");
scanf("%s", &login_pass);
}
}
printf("Login successful.\n");

fclose(fptr);

return 0;
}

Credentials.txt

---------------------

abc 123xyz

cba 124578


Related Solutions

Hi, I have created the following code and I was wondering if it is possible to...
Hi, I have created the following code and I was wondering if it is possible to make an "if" statement in the first for loop that would output an error if the user enters a float, character or string? I was thinking of something along the lines of: if(a[i] != int) but that didn't work :( Thank you. #include <iostream> using namespace std; // Creating a constant for the number of integers in the array const int size = 10;...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
Hi I was wondering if I could get an explanation on how race, ethnic groups, multiracial...
Hi I was wondering if I could get an explanation on how race, ethnic groups, multiracial people, minority groups, dominanat groups, prejudice, discrimination and racisism all relate to one another?
I am wondering how to calculate the consolidation entry for accumulated depreciation in the consolidation of...
I am wondering how to calculate the consolidation entry for accumulated depreciation in the consolidation of a less than wholly owned subsidiary acquired at more than book value with inventory transfers. Nothing I do seems to work, and I am getting really creative and confusing myself with all sorts of calculations. What is the proper formula for calculating consolidation entry for accumulated depreciation in this situation? Pop Corporation acquired 70 percent of Soda Company's voting common shares on January 1,...
Hello, I am wondering how I can determine the direction in which a wave is travelling...
Hello, I am wondering how I can determine the direction in which a wave is travelling given a function I must verify if it first is a solution to the wave equation by finding the partial derivatives then I do not know how to extrapolate the direction and speed of a wave. Thanks
Hi, I was wondering how to calculate the molar masses of these 2 compounds since my...
Hi, I was wondering how to calculate the molar masses of these 2 compounds since my instructions say to use my data and not the periodic table. The data is also listed below. Thanks! · Glycerol · NaCl · CaCl2 Data: H2O freezing point: 0 Glycerol freezing point: -1 NaCl freezing point: -5 CaCl2 freezing point: -4.3
I am currently working on this problem. It says it is wrong, and I was wondering...
I am currently working on this problem. It says it is wrong, and I was wondering if someone could correct my code to where its ouput is as followed. Expected Output: 6↵ 5↵ 4↵ 3↵ 2↵ 1 Actual Output: 9↵ 8↵ 7↵ 6↵ 5↵ 4↵ 3↵ 2↵ 1↵ 0↵ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values...
I am needing assistance figuring out how to write the code for this project in C#...
I am needing assistance figuring out how to write the code for this project in C# (C sharp)... Create a new Project and name it - InClassParticipation3 Get up to 5 inputs from the user. The user must enter at least 1 number. After the first input, ask them if they would like to input another number if they say 'yes' get the provided input and multiply it to the other inputs provided by the user. If the user says...
I was wondering if I was doing a conversion right. I am supposed to take aqueous...
I was wondering if I was doing a conversion right. I am supposed to take aqueous HNO3 with a density of 1.42 g/mL and a mass percent of solution of 70% and convert that into molarity. Would the correct conversion be to take 70 g of HNO3 over 100 g of H2O multiplied by 1 mol HNO3 over 63.01 g HNO3 (the molar mass) and then multiply that by 1 g H2O over 0.001 L of H2O to get the...
VB console .net framework I am missing something in my code below which will calculate factorials...
VB console .net framework I am missing something in my code below which will calculate factorials individually into the file to be read by a sub using theis outdated language please Not Sreamwriter or Reader fact*0, 2fact*0 Fact*1 , 2fact*1 Fact*2, 2fact*2 Fact*3, 2fact*3 (To 9) Only the first 9 (0-8) but what I have doe calculates only the 9 factorial giving a single value Public Function Calculate_fact(n_values As Integer) As Long 'calc local variables Dim fact As Long =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT