#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME 500
#define BIRTHDAY 500
#define HP 500
#define ADDRESS 500
#define OCCUPATION 500
#define SIZE 500
void Add();
void Del();
void Search();
void Edit();
void List();
void Exit();
char info[SIZE];
struct contactbook
{
char name [NAME];
char birthday [BIRTHDAY];
char hp [HP];
char address [ADDRESS];
char occupation [OCCUPATION];
char Tar [NAME];
}
contactbook;
FILE *Contactbook;
FILE *temp;
int main()
{
int a;
printf("Welcome to BRONTOK X Simple Contact Book\n");
printf("Please Choose your option\n");
printf("1: Adding The Contact\n");
printf("2: Delete The Contact\n");
printf("3: Search The Contact\n");
printf("4: Edit The Contact\n");
printf("5: List The Contact\n");
printf("6: Exit this Program\n");
scanf("%d", &a);
switch(a)
{
case 1:
{
Add();
break;
}
case 2:
{
Del();
break;
}
case 3:
{
Search();
break;
}
case 4:
{
Edit();
break;
}
case 5:
{
List();
break;
}
case 6:
{
Exit();
break;
}
default:
{
printf("Invalid Input");
}
}
}
void Add()
{
char opt1;
do
{
Contactbook = fopen("Contactbook.txt","a+");
printf("Enter his/her Name:\n");
scanf("%s", contactbook.name);
printf("Enter his/her Birthday:\n");
scanf("%s", contactbook.birthday);
printf("Enter his/her Handphone No:\n");
scanf("%s", contactbook.hp);
printf("Enter his/her Address:\n");
scanf("%s", contactbook.address);
printf("Enter his/her Occupation\n");
scanf("%s", contactbook.occupation);
fprintf(Contactbook, "%s %s %s %s %s\n", contactbook.name,
contactbook.birthday, contactbook.hp, contactbook.address,
contactbook.occupation);
fclose(Contactbook);
printf("To Continue using this program type Y/y");
scanf("%c", &opt1);
}
if(opt1 =='y' || opt1 == 'Y')
main();
}
void Del()
{
char opt2;
char Tar [SIZE];
int match = 0;
temp = fopen("temp.txt", "w");
if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
printf("----The file is empty----\n");
else
printf("Enter his/her name:");
gets(Tar);
while (!feof(Contactbook))
{
fscanf(Contactbook,"%s %s %s %s %s",
contactbook.name, contactbook.birthday, contactbook.hp,
contactbook.address, contactbook.occupation);
if(feof(Contactbook))
break;
if(strcmp(Tar, contactbook.name) !=0)
fprintf(temp,"%s %s %s %s %s\n", contactbook.name,
contactbook.birthday, contactbook.hp, contactbook.address,
contactbook.occupation);
else
{
match = 1;
}
if (!match)
{
printf("The name does not exist\n");
printf("or it might had been deleted\n");
}
fclose(Contactbook);
fclose(temp);
remove("Contactbook.txt");
rename("temp.txt","Contacbook.txt");
do
{
printf("To Continue using this program type Y/y");
scanf("%c", &opt2);
}
while (opt2 =='y' || opt2 == 'Y');
main();
}
}
void Search()
{
char opt3;
char Tar [SIZE];
int match = 0;
if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
printf("----The File is Empty----\n");
else
{
printf("Enter his/her name:\n");
scanf("%s", Tar);
while(!feof(Contactbook) && match ==0)
{
fscanf(Contactbook,"%s %s %s %s
%s",contactbook.name, contactbook.birthday, contactbook.hp,
contactbook.address, contactbook.occupation);
if(strcmp(Tar, contactbook.name) == 0)
match = 1;
}
if(match)
{
printf("His/Her Name is %s\n",contactbook.name);
printf("His/Her Birthday is s\n",
contactbook.birthday);
printf("His/Her Handphone No is %s\n",
contactbook.hp);
printf("His/Her Address is %s\n",
contactbook.address);
printf("His/Her Occupation is %s\n",
contactbook.occupation);
main();
}
else if (!match)
printf("----There is no such entry----");
fclose(Contactbook);
}
do
{
printf("To Continue using this program type Y/y");
scanf("%c", &opt3);
}
while (opt3 =='y' || opt3 == 'Y');
main();
}
void Edit()
{
void List()
{
int opt5;
Contactbook = fopen("Contactbook.txt","a+");
do
{
fgets(info,SIZE,Contactbook);
printf("%s\n", info);
}
while(!feof(Contactbook));
fclose(Contactbook);
do
{
printf("Enter y or Y to run the program again\n");
scanf("%c", &opt5);
}
while (opt5 == 'y' ||opt5 == 'Y');
main();
}
void Exit()
{
printf("Thank You For Using my Contactbook. Have a Nice
Day");
}