#include<stdio.h>
#include<conio.h>
#include<string.h>
struct emp
{
 char name[20];
 char tel[20];
}list[20];
FILE *cfptr;
int size,ch;
/************************************************************************/
void add (struct emp in[],int &count)
{
 clrscr();
 printf("\n\n\nEnter user name: ");
 fflush(stdin);
 gets(in[count].name);
 printf("\n\n\nEnter user phone: ");
 scanf("%s",&in[count].tel);
 count++;
}
/************************************************************************/
void del (struct emp in[],int &count)
{
 char nm[20];
 clrscr();
 printf("Enter the name to delete: \n\n\n");
 fflush(stdin);
 gets(nm);
 for (int j=0;j<count;j++)
  if (strcmpi(nm,in[j].name)==0)
   for(int i=j;i<count;i++)
    in[i]=in[i+1];
 count--;
}
/************************************************************************/
void update(struct emp in[],int count)
{int ch;char tel[20];
 char nm[20];
 do
 {
  clrscr();
  printf("1. update name\n\n\n2.update phone\n\n\n3.Exit\n\n\n");
  printf("Enter your choice: ");
  scanf("%d",&ch);
  switch (ch)
  {
   case 1:printf("\n\n\nEnter the old name: ");
	  fflush(stdin);
	  gets(nm);
	  for (int j=0;j<count;j++)
	   if (strcmpi(nm,in[j].name)==0)
	   {
	    clrscr();
	    printf("\n\n\nEnter the new user name : ");
	    gets(in[j].name);
	   }
	   break;
   case 2:printf("\n\n\nEnter the old phone number: ");
	  scanf("%s",&tel);
	  for ( j=0;j<count;j++)
	   if (tel==in[j].tel)
	   {
	    clrscr();
	    printf("\n\n\nEnter the new phone number: ");
	    scanf("%s",&in[j].tel);
	   }
	   break;
  }
 } while (ch!=3);
}
/************************************************************************/
void display(struct emp in[],int count)
{clrscr();
  for (int i=0;i<count;i++)
  printf("%-20s%-10s\n",in[i].name,in[i].tel);
 getch();
}
/************************************************************************/
void sort(struct emp in[],int count)
{struct emp  temp;
 for (int j=0;j<count;j++)
 for (int i=j+1;i<count;i++)
 if (strcmpi(in[j].name,in[i].name)>0)
 {
  temp=in[j];
  in[j]=in[i];
  in[i]=temp;
 }
 display(in,count);
}
/************************************************************************/
void save (FILE *sta,struct emp in[],int count)
{
 for (int i=0;i<count;i++)
 fprintf(sta,"%-20s%-10s\n",in[i].name,in[i].tel);
}
/************************************************************************/
void load (FILE *sta,struct emp in[])
{ struct emp temp;
 int size=0;
 while (!feof(sta))
 {
     fread( &temp, sizeof( struct emp  ), 1, cfptr );
     strcpy(in[size].name,temp.name);
     strcpy(in[size].tel,temp.tel);
  size++;
 }
}
/************************************************************************/
void main ()
{
 int size=0;
 char d,c;
 clrscr();
 printf("1. Run for first time\n\n\n2. Run for second time\n\n\n enter your choice: ");
 scanf("%d",&ch);
 switch(ch)
 {
  case 1:cfptr=fopen("c:\\stud.dat","w");
	  if (cfptr==NULL)
{	  printf("ERROR : Can't opin this file");
	  getch(); }
	  break;
  case 2:cfptr=fopen("c:\\stud.dat","a");
	 if (cfptr==NULL)
	  {
	  printf("ERROR : Can't opin this file");
	  getch();}
	  load(cfptr,list);
	  fclose(cfptr);
	  fopen("c:\\stud.dat","w");
	  save(cfptr,list,size);
	  break;
 }
 do
 {
  clrscr();
    printf("    This Program Edit By:______________\n\n\n\n");
  printf("		    1. Add\n\n\n"
	 "		    2. Display\n\n\n"
	 "		    3. Sort\n\n\n"
	 "		    4. Delete\n\n\n"
	 "		    5. Update\n\n\n"
	 "                    6. Exit\n\n");
  printf("\nEnter your choice : ");
  scanf("%d",&ch);
  switch (ch)
  {
   case 1:add(list,size);break;
   case 2:display(list,size);break;
   case 3:sort(list,size);break;
   case 4:del(list,size);break;
   case 5:update(list,size);break;
   case 6:{
	   clrscr();
	   printf("\n\n\nDo you want save ( Y / N ) ? ");
	   fflush(stdin);
	   scanf("%c",&d);
	   switch(d)
	   {
	    case 'y':cfptr=fopen("c:\\stud.dat","a");
		     save(cfptr,list,size);
		     ch++;
		     break;
	    case 'Y':cfptr=fopen("c:\\stud.dat","a");
		     save(cfptr,list,size);
		     ch++;
		     break;
	    default:ch++;
	    }
  }break;
 }
}while(ch!=7);
fclose(cfptr);
}


















