Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.
com/programcodes
Phone DIRECTORY PROGRAM
IN CPP using linked list and
FILE HANDLING
-->Save data in file
-->add new number
-->delete any number from current list
-->search and modify
-->see all list (reading from file )
-->see current numbers added
By DJVprogrammers
https://web.facebook.com/DJVprogrammers 1
Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.com/programcodes
Code:
//Phone directory program using linked list and file handling
#include<iostream>
#include<fstream>
#include<conio.h>
using namespace std;
fstream myfile;
struct clients
//parts of a node.
string name;
string number;
string cnic;
clients*link;
clients()
link=NULL;
};
class directory
https://web.facebook.com/DJVprogrammers 2
Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.com/programcodes
//pointers to handle the linklist
clients *start,*cur,*temp;
public:
directory()
start=NULL;
//function to create nodes
creat_file (string n,string num,string cn)
//create 1st node...
if(start==NULL)
start=new clients;
start->link=NULL;
start->name=n;
start->number=num;
start->cnic=cn;
https://web.facebook.com/DJVprogrammers 3
Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.com/programcodes
myfile.open ("directory.txt",ios::out | ios::app);
myfile<<"Name_:_"<<start->name<<"__|__CNIC:_"<<start->cnic<<"__|__Phone
Number_:_"<<start->number<<endl;
myfile.close();
//to create new node..
else
cur=start;
while(cur->link!=NULL)
cur=cur->link;
temp=new clients;
temp->name=n;
temp->number=num;
temp->cnic=cn;
temp->link=NULL;
cur->link=temp;
myfile.open ("directory.txt",ios::out | ios::app);
https://web.facebook.com/DJVprogrammers 4
Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.com/programcodes
myfile<<"Name_:_"<<temp->name<<"__|__CNIC:_"<<temp->cnic<<"__|__Phone
Number:_"<<temp->number<<endl;
myfile.close();
void terminating_file(string n)
cur=start;
if(start->name==n)
temp=start;
start=start->link;
delete temp;
else
while(cur->name!=n)
if(cur->link==NULL)
{
https://web.facebook.com/DJVprogrammers 5
Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.com/programcodes
cout<<"Any case of"<<n<<" does not exist"<<endl;
return ;
temp=cur;
cur=cur->link;
temp->link=cur->link;
delete cur;
//print data of the linklist.
void print_now()
cur=start;
int c;
cout<<"Person #"<<" ---> Name \tNumber \tCNIC"<<endl;
for(c=1;cur->link!=NULL;c++)
https://web.facebook.com/DJVprogrammers 6
Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.com/programcodes
cout<<"Person "<<c<<" ---> "<<cur->name<<"\t"<<cur->number<<"\t"<<cur->cnic<<endl;
cur=cur->link;
cout<<"Person "<<c<<" ---> "<<cur->name<<"\t"<<cur->number<<"\t"<<cur->cnic<<endl;
void search(string n)
cur=start;
while(cur!=NULL)
if(cur->name==n)
char mod;
cout<<"\n\t_________DATA FOUND__________\n\n";
cout<<"Name : "<<cur->name<<endl;
cout<<"CNIC : "<<cur->cnic<<endl;
cout<<"Phone # : "<<cur->number<<endl;
cout<<"\nDo You want to modify Phone Number (y/n) : ";
cin>>mod;
if(mod=='y' || mod=='Y')
https://web.facebook.com/DJVprogrammers 7
Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.com/programcodes
cout<<"Enter New Phone Number ";
cin>>cur->number;
cout<<"\n\t_________Data Updated________"<<endl;
break;
cur=cur->link;
if(cur==NULL)
cout<<"\n\t_______DATA NOT FOUND_________\n\n";
void print()
//this function reads the data from file
string s;
myfile.open("directory.txt",ios::in);
while(!myfile.eof())
myfile>>s;
https://web.facebook.com/DJVprogrammers 8
Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.com/programcodes
cout<<s<<endl;
myfile.close();
};
int main()
directory l1;
int nooffiles,i,p,ch;
string n,nm,cnc;
cout<<"\n\t=============================================================
======\n\n";
cout<<"\t\t\t PHONE DIRECTORY SYSTEM\n";
cout<<"\n\t=============================================================
======\n\n";
cout<<"\n\t=============================================================
======\n\n";
cout<<"\n\t1- SAVE NUMBERS\n";
cout<<"\n\t2- CAN SEARCH AND UPDATE THE NUMBER\n";
https://web.facebook.com/DJVprogrammers 9
Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.com/programcodes
cout<<"\n\t3- WHEN A NUMBER IS OFF,,, YOU CAN DELETE IT FROM YOUR
DIRECTORY\n";
cout<<"\n\t4-CAN SEE ALL THE LIST ANY TIME\n";
cout<<"\n\t=============================================================
======\n\n";
cout<<"\n\t========================SAVE
NUMBERs===============================\n"<<endl;
//no. of files.
cout<<"\thow many numbers you want to save"<<endl;
cin>>nooffiles;
//create/save data of each memeber....function call pass data..
for(i=1;i<=nooffiles;i++)
cout<<"\n\tEnter Name \t";
//getline(cin,n);
cin>>n;
cout<<"\n\tEnter Phone number \t";
cin>>nm;
cout<<"\n\tEnter CNIC \t";
cin>>cnc;
l1.creat_file(n,nm,cnc);
https://web.facebook.com/DJVprogrammers 10
Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.com/programcodes
system("CLS");
do
cout<<"\n\nWhat You want to do with your cases now"<<endl;
cout<<"\n1-Save new phone number\n";
cout<<"2-Delete any number"<<endl;
cout<<"3-Search and Modify"<<endl;
cout<<"4-See All list"<<endl;
cout<<"5-Exit\n"<<endl;
cin>>ch;
system("CLS");
switch(ch)
case 1:
cout<<"\n\t============================SAVE NEW
NUMBER============================\n\n";
cout<<"Enter Data to insert new person's data'"<<endl;
cout<<"\nEnter Name \t";
//getline(cin,n);
https://web.facebook.com/DJVprogrammers 11
Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.com/programcodes
cin>>n;
cout<<"\nEnter Phone number \t";
cin>>nm;
cout<<"Enter CNIC \t";
cin>>cnc;
l1.creat_file(n,nm,cnc);
cout<<"Number of "<<n<<" has been Saved in your phone directory "<<endl;
break;
case 2:
cout<<"\n\t=========================DELETE
NUMBER============================\n\n";
cout<<"\nEnter name of person whose number you want to delete\n"<<endl;
//getline(cin,n);
cin>>n;
l1.terminating_file(n);
cout<<"\nNumber of "<<n<<" Has been removed from your list"<<endl;
break;
case 3:
cout<<"\n\t=====================SEARCH AND UPDATE
NUMBER=====================\n\n";
cout<<"Enter NAME of Person to search it from list\t";
https://web.facebook.com/DJVprogrammers 12
Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.com/programcodes
cin>>n;
l1.search(n);
break;
case 4:
cout<<"\n\t=====================PRINTING ALL
LIST============================\n\n";
l1.print();
break;
case 5:
cout<<"\n\t====================LIST OF Numbers added
now======================\n\n";
l1.print_now();
break;
default:
cout<<"\t----------Invalid----------"<<endl;
while(ch!=5);
cout<<"\nTHANK YOU"<<endl;
getch();
https://web.facebook.com/DJVprogrammers 13
Program.codes.cpp@gmail.com. https://programcodescpp.wixsite.com/programcodes
Website:
https://programcodescpp.wixsite.com/programcodes
Email:
Program.codes.cpp@gmail.com
Facebook Page:
https://web.facebook.com/DJVprogrammers
YouTube Channel
https://www.youtube.com/channel/UCfizosx-0fkFJ6R-oF6l9-A?view_as=subscriber
https://web.facebook.com/DJVprogrammers 14