You are on page 1of 15

Assignment:

Create an address book directory in C with the following functions:


1. The possibility to append new members in the address book
2. The possibility to search for members by ID, name, surname or both name and surname
3. The possibility to delete members from the address book
4. The possibility to modify member data
The data should be saved in a file.
SOLUTION
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct person
{
char id[20];
char name[20];
char surname[20];
char tel[20];
char mail[20];
}p;
FILE*fp;
int find_number_of_records()
{
fp=fopen("records.txt","r");
int n=0;
while(fread(&p,sizeof p,1,fp))
{
n++;
}
return n;
}
void append()
{
fp=fopen("records.txt","a");
if (fp==NULL)
{
printf("Error in file access\n");
exit(0);
}
printf("Enter ID:\n");
gets(p.id);
printf("Enter name:\n");
gets(p.name);
printf("Enter surname:\n");
gets(p.surname);
printf("Enter telephone number:\n");
gets(p.tel);
printf("Enter e-mail address:\n");
gets(p.mail);
fwrite(&p,sizeof p, 1,fp);
fclose(fp);
}
void search_by_ID()
{
fp=fopen("records.txt","r");
if (fp==NULL)
{
printf("Error in file access\n");
exit(0);
}
char ids[20];
printf("Enter the ID to be searched:\n");
gets(ids);
int count=0;
while(fread(&p,sizeof p, 1, fp)==1)
{
if (strcmp(ids,p.id)==0)
{
count++;
printf("ID: %s \n",p.id);
printf("Name: %s \n",p.name);
printf("Surname: %s \n",p.surname);
printf("Telephone number: %s \n",p.tel);
printf("E-mail address: %s \n",p.mail);
}
}
if (count==0)
{
printf("No record found\n");
}
}
void search_by_name()
{
fp=fopen("records.txt","r");
if (fp==NULL)
{
printf("Error in file access\n");
exit(0);
}
char names[20];
printf("Enter the name to be searched:\n");
gets(names);
int count=0;
while(fread(&p,sizeof p, 1, fp)==1)
{
if (strcmp(names,p.name)==0)
{
count++;
printf("ID: %s \n",p.id);
printf("Name: %s \n",p.name);
printf("Surname: %s \n",p.surname);
printf("Telephone number: %s \n",p.tel);
printf("E-mail address: %s \n",p.mail);
}
}
if (count==0)
{
printf("No record found\n");
}
}
void search_by_surname()
{
fp=fopen("records.txt","r");
if (fp==NULL)
{
printf("Error in file access\n");
exit(0);
}
char surnames[20];
printf("Enter the surname to be searched:\n");
gets(surnames);
int count=0;
while(fread(&p,sizeof p, 1, fp)==1)
{
if (strcmp(surnames,p.surname)==0)
{
count++;
printf("ID: %s \n",p.id);
printf("Name: %s \n",p.name);
printf("Surname: %s \n",p.surname);
printf("Telephone number: %s \n",p.tel);
printf("E-mail address: %s \n",p.mail);
}
}
if (count==0)
{
printf("No record found\n");
}
}
void search_by_name_and_surname()
{
fp=fopen("records.txt","r");
if (fp==NULL)
{
printf("Error in file access\n");
exit(0);
}
char names[20], surnames[20];
printf("Enter the name to be searched:\n");
gets(names);
printf("Enter the surname to be searched:\n");
gets(surnames);
int count=0;
while(fread(&p,sizeof p, 1, fp)==1)
{
if (strcmp(names,p.name)==0 &&
strcmp(surnames,p.surname)==0)
{
count++;
printf("ID: %s \n",p.id);
printf("Name: %s \n",p.name);
printf("Surname: %s \n",p.surname);
printf("Telephone number: %s \n",p.tel);
printf("E-mail address: %s \n",p.mail);
}
}
if (count==0)
{
printf("No record found\n");
}
}
void del()
{
char idd[20];
printf("ID to be deleted:\n");
gets(idd);
fp=fopen("records.txt","r");
int n;
n=find_number_of_records();
struct person people[n];
fp=fopen("records.txt","r");
int i;
for ( i=0;i<n;i++)
{
fread(&p,sizeof p,1,fp);
people[i]=p;
}
int j;
fclose(fp);
int count=0;
for (i=0;i<n;i++)
{
if (strcmp(idd,people[i].id)==0)
{
for (j=i;j<n-1;j++)
{
people[j]=people[j+1];
}
n--;
count ++;
}
}
if (count==0)
printf("Record not found\n");
else
{
fp=fopen("records.txt","w");
for (i=0;i<n;i++)
{
p=people[i];
fwrite(&p,sizeof p,1,fp);
}
fclose(fp);
}
}
void modify()
{
fp=fopen("records.txt","r");
//Determine the number of records in file
int n;
n=find_number_of_records();
struct person people[n];
fp=fopen("records.txt","r");
int i;
for ( i=0;i<n;i++)
{
fread(&p,sizeof p,1,fp);
people[i]=p;
}
char idm[20];
printf("Enter the ID to be modified:\n");
gets(idm);
fclose(fp);
int count=0;
for (i=0;i<n;i++)
{
if (strcmp(idm,people[i].id)==0)
{
count++;
printf("Do you want to modify the ID? (Y/N)\n");
char c;
c=getch();
if (c=='y'||c=='Y')
{
printf("New ID:\n");
gets(people[i].id);
}
printf("Do you want to modify the name?
(Y/N)\n");
c=getch();
if (c=='y'||c=='Y')
{
printf("New name:\n");
gets(people[i].name);
}
printf("Do you want to modify the surname?
(Y/N)\n");
c=getch();
if (c=='y'||c=='Y')
{
printf("New surname:\n");
gets(people[i].surname);
}
printf("Do you want to modify the telephone
number? (Y/N)\n");
c=getch();
if (c=='y'||c=='Y')
{
printf("New telephone number:\n");
gets(people[i].tel);
}
printf("Do you want to modify the e-mail address?
(Y/N)\n");
c=getch();
if (c=='y'||c=='Y')
{
printf("New e-mail address:\n");
gets(people[i].mail);
}
}
}
if (count==0)
{
printf("Record not found\n");
}
else{
fp=fopen("records.txt","w");
for (i=0;i<n;i++)
{
p=people[i];
fwrite(&p,sizeof p,1,fp);
}}
}
//MENU
main()
{
char c;
do
{
printf("***ADDRESS BOOK DIRECTORY***\n");
printf("1.Append\n2.Search\n3.Delete\n4.Modify\n5.Sort\n");
char z;
z=getch();
switch(z)
{
case '1': append(); break;
case '2':
{
char choice;
printf("1.Search by ID\n2.Search by
name\n3.Search by surname\n4.Search by name and surname\n");
choice=getch();
switch(choice)
{
case '1': search_by_ID(); break;
case '2':search_by_name(); break;
case '3':search_by_surname(); break;
case
'4':search_by_name_and_surname();break;
}
} break;
case '3': del(); break;
case '4': modify(); break;
}
printf("Do you want to continue? (Y/N) \n");
c=getch();
}
while(c=='y'||c=='Y');
}

You might also like