Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cctype>
#define MAX2 20
#define MAX1 5
typedef struct{
char user_name[MAX2],password[MAX2];
int privilige;
}account;
void display(account *,FILE *);
account add_account();
void delete_account(account *, int);
void change_password(account *);
void sort (account [] , int);
int main(){
int account_number = 0,choice1,choice2,choice3,choice4,choice5;
FILE *ptr;
if((ptr = fopen("accounts.bin","r+")) || (ptr = fopen("accounts.bin","w"))){
do{
printf("Ready to manipulate your data.\n");
printf("\t\tTo add accounts, press 1.\n");
printf("\t\tTo delete an account, press 2.\n");
printf("\t\tTo change a password, press 3.\n");
printf("\t\tTo display details of an account, press 4.\n");
printf("\t\tTo exit, press 5.\n");
printf("\t\t\tYour choice?");
scanf("%d",&choice1);
switch(choice1){
case 1:
printf("\nHow many accounts you want to add?");
scanf("%d",&choice2);
account_number += choice2;
account temp[account_number];
for (int i = 0 ; i < choice2 ; i++)
temp [i] = add_account();
for (int j = choice2 ; j < account_number ; j++)
fread(&temp[j],sizeof(account),1,ptr);
sort(temp,account_number);
for(int k = 0 ; k < account_number ; k++)
fwrite(&temp[k],sizeof(account),1,ptr);
break;
case 2:
if(!account_number){
printf("Cannot delete account.Your database is empty\n.");
exit(1);
}
account temp2[account_number];
do{
printf("\nType the account's corresponding user name.\n");
account temp;
fgets(temp.user_name,MAX1,stdin);
if(account_number){
delete_account(&temp,account_number);
account_number--;
}
else{
printf("Cannot delete. Database already empty.\n");
exit(1);
}
printf("Again?(1 to remove more, another key to exit):");
scanf("%d",&choice3);
}while(choice3 == 1);
for (int j = 0 ; j < account_number ; j++)
fread(&temp2[j],sizeof(account),1,ptr);
sort(temp2 , account_number);
for(int k = 0 ; k < account_number ; k++)
fwrite(&temp2[k],sizeof(account),1,ptr);
break;
case 3:
do{
if(!account_number){
printf("Database is empty.\n");
exit(1);
}
printf("\nType the account's corresponding user name.\n");
account temp;
fgets(temp.user_name,MAX1,stdin);
change_password(&temp);
printf("Password changed.\n");
printf("again?(1 to change more passwords, any other key to exits):");
scanf("%d",&choice4);
}while(choice4 == 1);
case 4 :
do{
account temp;
printf("Enter the name of the account.\n");
fgets(temp.user_name,MAX2,stdin);
display(&temp,ptr);
printf("More details?(1 to confirm, else to exit):");
scanf("%d",&choice5);
}while(choice5 == 1);
case 5:
printf("You existed the program.\n");
fclose(ptr);
return 0;
default:
printf("Invalid Input.\n");
}
}while(1);
}
else{
printf("couldn't open the file.\n");
exit(1);
}
printf("\n");
system("PAUSE");
return 0;
}
void display(account *x,FILE *ptr){
account temp;
strcpy(temp.user_name , "doesn't exist.");
strcpy(temp.password , "Doesn't exist.");
temp.privilige = 4;
int i = 0;
while(!feof(ptr)){
fread(&temp,sizeof(account),1,ptr);
if(!strcmp(temp.user_name , x->user_name)){
temp = *x;
exit(1);
}
}
printf("\tThe name is: %s\n",temp.user_name);
printf("\tThe password is: %s\n",temp.password);
switch(temp.privilige){
case 1 :
printf("\tThe privilige is admin.\n");
break;
case 2:
printf("\tThe privilige is: general\n");
break;
case 3:
printf("\tThe privilige is mail-only.\n");
break;
default :
printf("Doesn't exist.\n");
}
}
account add_account(){
account x;
printf("\tEnter the user name:");
fgets(x.user_name,MAX2,stdin);
printf("\tenter the password");
int a = 0;
do{
fgets(x.password,MAX2,stdin);
int s = strlen(x.password);
for (int i = 0 ; i < s - 1 ; i++){
if(!isalpha(x.password[i])){
a = 1;
break;
}
}
}while(!a);
printf("\tEnter the privilige: (1 for admin, 2 for general, and 3 for mail-only)");
scanf("%d",&x.privilige);
while(x.privilige != 1 && x.privilige != 2 && x.privilige != 3){
printf("Invalid privilige.Enter again:");
scanf("%d",&x.privilige);
}
return x;
}
void change_password(account *x){
printf("Enter the passowrd for %s(should contain at least one non_alpha char)\n",x->user_name);
int a = 0;
char temp[MAX2];
fgets(temp,MAX2,stdin);
if (!strcmp(temp,x->password)){
printf("confirmation:");
fgets(temp,MAX2,stdin);
if (!strcmp(temp,x->password)){
printf("Ok, now enter the new password:");
do{
fgets(temp,MAX2,stdin);
int s = strlen(temp);
for (int i = 0 ; i < s - 1 ; i++){
if(!isalpha(temp[i])){
a = 1;
break;
}
}
}while(!a);
strcpy(x->password,temp);
printf("The password is now changed.\n");
}
else{
printf("The entered password and the confirmation do not match each other.\n");
}
}
}
void sort(account x[],int n){
int result;
int min;
for (int i = 0 ; i < n - 1 ; i++){
min = i;
for (int j = i + 1 ; j < n; j++){
if ((result = strcmp(x[i].user_name,x[j].user_name)) > 0)
min = j;
}
account temp;
temp = x[i];
x[i] = x[min];
x[min] = temp;
}
}