You are on page 1of 4

#include<stdio.

h>
#include<string.h>
#include<conio.h>
void main()
{
struct contacts
{
long int tel;
char name[25];
char email[50];
int n;
};
FILE *fp,*ft;
int choice;
char ch;
char na[25];
struct contacts c;
long int recsize;
recsize=sizeof(c);
do
{
printf("\n menu:");
printf("\n 1:add contact");
printf("\n 2:delete contact");
printf("\n 3:show all contacts");
printf("\n 4:delete all");
printf("\n 5:exit");
printf("\n Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
do
{
fp=fopen("contact.dat","a+");
printf("\n\n Enter name :");
scanf("%s",c.name);
printf("\n Telephone no. ");
scanf("%ld",&c.tel);
printf("\n Email :");
scanf("%s",c.email);
fwrite(&c,recsize,1,fp);
fclose(fp);
fflush(stdin);
printf("\n\n Do you want to enter another record
? ");
scanf("%c",ch);
fflush(stdin); //
}while(ch=='y');
break;
case 2:
fp=fopen("contact.txt","r+");
printf("\n Enter name whose contact is to be del
eted ");
scanf("%s",na);
ft=fopen("temp.txt","w");
rewind(fp);
while(fread(&c,recsize,1,fp)==1)
if(strcmp(c.name,na)!=0)
fwrite(&c,recsize,1,ft);
fclose(fp);
fclose(ft);
remove("contact.txt");
rename("temp.txt","contact.txt");
printf("\n Contact deleted successfully ");
fflush(stdin); //
getch();
break;
case 3 :
fp=fopen("contact.txt","r");
rewind(fp);
while(fread(&c,recsize,1,fp)==1)
{
printf("\n %s : %ld : %s ",c.name,c.tel,
c.email);
}
fclose(fp);
fflush(stdin);
getch();
break;
case 4:
printf("\n Deleting entire database ");
fp=fopen("contact.txt","w");
fclose(fp);
fflush(stdin); //
getch();
break;
case 5:
exit(1);
default :
printf("\n Invalid choice ");
fflush(stdin); //
getch();
break;

}
}
while(choice!=5);
}

OUTPUT
menu:
1:add contact
2:delete contact
3:show all contacts
4:delete all
5:exit
Enter your choice: 1

Enter name : Chinmay


Telephone No. : 25288292
Email : cbarve@hotmail.com
Do you want to enter another record ? y
Enter name : Ranjit
Telephone No. : 25327512
Email : ranjit.salvi@gmail.com
Do you want to enter another record ? n

menu:
1:add contact
2:delete contact
3:show all contacts
4:delete all
5:exit
Enter your choice: 3
Chinmay : 25288292 : cbarve@hotmail.com
Ranjit : 25327512 : ranjit.salvi@gmail.com
menu:
1:add contact
2:delete contact
3:show all contacts
4:delete all
5:exit
Enter your choice: 2
Enter name whose contact is to be deleted : Ranjit
Contact deleted successfully
menu:
1:add contact
2:delete contact
3:show all contacts
4:delete all
5:exit
Enter your choice: 3
Chinmay : 25288292 : cbarve@hotmail.com
menu:
1:add contact
2:delete contact
3:show all contacts
4:delete all
5:exit
Enter your choice: 4
Deleting entire database
menu:
1:add contact
2:delete contact
3:show all contacts
4:delete all
5:exit
Enter your choice: 3
menu:
1:add contact
2:delete contact
3:show all contacts
4:delete all
5:exit
Enter your choice: 5

You might also like