Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
typedef struct account
{
char name[20];
char password[20];
char previleges[20];
}account;
void sort_account(char* );
void fill_account(account);
int choice(void);
void account_write(char *,account);
void account_search(char *);
void account_delete(char *);
void change_previlege(char *);
void account_write2(char *,account );
void view_account(char *);
void change_password(char *);
void change_countype(char *);
int main(){
account c;
int n;
char *filename="account.txt";
int option;
while( (option=choice())!=8)
{
switch(option){
case 1: account_write(filename,c);
break;
case 2: account_search(filename);
break;
case 3: account_delete(filename);
break;
case 4: sort_account(filename);
case 5: view_account(filename);
break;
case 6: change_password(filename);
break;
case 7: change_previlege(filename);
default: printf("unavailable option");
}
//view_account(filename,n);
}
}
void fill_account(account *c)
{
int flag,i=0,sum=0;
printf("give the name of account to add");
scanf("%s",&c->name);
printf("give the password");
scanf("%s",&c->password);
while (sum==0)
{
while(c->password[i])
{
if(isdigit(c->password[i]))
sum++;
i++;
}
if(sum==0)
{
printf("re-enter the password the last one is wrong");
scanf("%s",&c->password);
}
}
printf("give previliges");
scanf("%s",&c->previleges);
}
void account_write(char *filename,account c)
{
FILE *file=fopen(filename,"ab");
fflush(file);
if(file!=NULL)
{
fill_account(&c);
fwrite(&c,sizeof (account),1,file);
}
fclose(file) ;
}
void account_search(char *filename)
{int count=0,i=0,j=0;
char NAME[20];
account t[20];
FILE *file=fopen(filename,"rb");
if(file!=NULL)
{
while(fread(&t[i],sizeof (account),1,file)==1)
{
i++;
count++;
}
}
printf("give the name of accountto search for");
scanf("%s",&NAME);
for(j=0;j<count;j++)
{
if(strcmp(t[j].name,NAME)==0)
printf("%s \t %s\t %s \t",t[j].name,t[j].password,t[j].previleges);
}
fclose(file);
}
void view_account(char *filename)
{
int count=0;
int i=0;
account t[30];
FILE *file=fopen(filename,"rb");
if(file!=NULL)
{
while(fread(&t[i],sizeof (account),1,file)==1)
{
i++;
count++;
}
}
printf( "Acc.Name | Password| Privilege");
for(i=0;i<count;i++)
{
printf("\n %s | %s | %s\n ",t[i].name,t[i].password,t[i].previleges);
}
}
void account_delete(char *filename)
{
int count=0,i=0,g,h;
char NAME[20];
account t[20];
FILE *file=fopen(filename,"rb");
if(file!=NULL)
{
while(fread(&t[i],sizeof (account),1,file)==1)
{
i++;
count++;
}
}
printf("give the name of account to delete");
scanf("%s",&NAME);
for( g=0;g<count;g++)
{if(strcmp(t[g].name,NAME)==0)
{ for(h=g;h<count;h++)
{
t[h]=t[h+1];
}
}
}
count--;
for(i=0;i<count;i++)
{
account_write2(filename,t[i]);
}
view_account(filename);
fclose(file);
}
void account_write2(char *filename,account c )
{
FILE *file=fopen(filename,"wb");
fflush(file);
if(file!=NULL)
{
fwrite(&c,sizeof (account),1,file);
}
fclose(file) ;
}
void change_password(char *filename)
{
int count=0,i=0;
char pwd[20];
char pwd1[20];
char pwd2[20];
char NAME[20];
account t[20];
int k,j;
FILE *file=fopen(filename,"rb");
if(file!=NULL)
{
while(fread(&t[i],sizeof (account),1,file)==1)
{
i++;
count++;
}
}
printf("give the name of accountyou want to change the password");
scanf("%s",&NAME);
for(j=0;j<count;j++)
{
if(strcmp(t[j].name,NAME)==0)
{
printf("give the old password (contains at least one alphabetic character)" );
scanf("%s",&pwd);
if(strcmp(t[j].password,pwd)==0)
{ printf("give the new password ");
scanf("%s",&pwd1);
printf("confirm the new password ");
scanf("%s",&pwd2);
}
if(strcmp(pwd1,pwd2)==0) strcpy(t[j].password,pwd1);
}
}
FILE *file1=fopen(filename,"wb");
for(int i=0;i<count;i++)
{
if(file1!=NULL)
{
fwrite(&t[i],sizeof (account),1,file1);
}
}
fclose(file1);
view_account(filename);
}
int choice (void) {
int menu;
printf("Please enter your choice :\n\n");
printf("\t1- Create a new account\n"
"\t2- View information about an account\n"
"\t3- delete an acount\n"
"\t4- view all acou\n"
"\t- sort\n"
"\t5- Change the privilege of an account\n"
"\t6- End programm\n?");
scanf("%d",&menu);
return(menu);
}
void change_previlege(char *filename)
{
int i=0,count=0;
char pass[20],name[20];
char previleges[20];
account t[20];
FILE *file=fopen(filename,"rb");
if(file!=NULL)
{
while(fread(&t[i],sizeof (account),1,file)==1)
{
i++;
count++;
}
}
printf("give the name for wich you would like to change the previleges\n");
scanf("%s",&name);
printf("give the password");
scanf("%s",&pass);
for (int i=0;i<count;i++)
{
if ((strcmp(pass,t[i].password)==0)&&(strcmp(name,t[i].name)==0))
{
printf("give the new previlege");
scanf("%s",&previleges);
strcpy(t[i].previleges,previleges);
}
}
fclose(file);
FILE *file1=fopen(filename,"wb");
for(int i=0;i<count;i++)
{
if(file1!=NULL)
{
fwrite(&t[i],sizeof (account),1,file1);
}
}
fclose(file1);
view_account(filename);
}
void sort_account(char *filename)
{
int i,j,count=0;
FILE *file=fopen(filename,"rb");
account t[30];
account temp;
if(file!=NULL)
{
while(fread(&t[i],sizeof (account),1,file)==1)
{
i++;
count++;
}
}
for (i=0; i<count; ++i)
{ for (j = i+1; j <count ; ++j)
{
if (strcmp(t[i].name,t[j].name)>0)
{
temp=t[i];
t[i]=t[j];
t[j]=temp;
}
}
}
view_account(filename);
fclose(file);
}