Write a program that will create data file containing the list of telephone numbers
Answers
#include<fstream>
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
int main()
{
int n,cnt=0;
char name[10],aname[10];
char id[10],findid[10];
int ch;
char ans;
ofstream fout;
ifstream fin;
// Writing data into file.......
fout.open("data1.txt");
if(!fout)
{
cout<<"Error in opening file ";
return 0;
}
cout<<"File created successfully...\n";
cout<<"How many no of records you want to insert: ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\nEnter the name: ";
cin>>name;
cout<<"\nEnter the id: ";
cin>>id;
cout<<"\nEnetr the author of book:";
cin>>aname;
fout <<"\n"<<setw(20)<<id <<" "<<setw(20)<<name<<" "<<setw(20)<<aname;
cnt++;
}
fout.close();
cout<<"\nFile saved snd closed successfully\n";
// Reading data from file......
do
{
cout<<"1.Puchase new book\n";
cout<<"2.Return book\n";
cout<<"Enter choice ";
cin>>ch;
switch(ch)
{
case 1:
fin.open("data1.txt");
if(!fin)
{
cout<<"Error in opening file ";
return 0;
}
cout<<"\nEnter the id of book: ";
cin>>findid;
cout<<"\nFile contents..";
for(int i=0;i<cnt;i++)
{
fin>>id;
if(strcmp(id,findid)==0)
{
fin>>name;
fin>>aname;
cout<<"\nName: "<<name;
cout<<"\nAuthor: \n"<<aname;
cout<<"\nId: \n"<<id;
}
}
fin.close();
break;
case 2:
fout.open("data1.txt",ios::app);
if(!fout)
{
cout<<"Error in opening file ";
return 0;
}
cout<<"How many no of books you want to return (1 or 2): ";
cin>>n;
if(n>2)
{
cout<<"Invalid...";
}
else
{
for(int i=0;i<n;i++)
{
cout<<"\nEnter the name: ";
cin>>name;
cout<<"\nEnter the id: ";
cin>>id;
cout<<"\nEnetr the author of book:";
cin>>aname;
fout <<"\n"<<setw(20)<<id <<" "<<setw(20)<<name<<" "<<setw(20)<<aname;
}
cout<<"Return successfully...";
}
fout.close();
break;
}
cout<<"You want to serch more data ? ";
cin>>ans;
}while(ans=='y' || ans=='Y');
return 0;
}
/*----------------------------------------------------------
Output
[student@localhost Pratiksha]$ g++ filemobile.cpp
[student@localhost Pratiksha]$ ./a.out
File created successfully...
How many no of records you want to insert: 3
Enter the name: Pratiksha
Enter the phone number: 123456789
Enter the name: Ankita
Enter the phone number: 98765432
Enter the name: tayi
Enter the phone number: 2345665432
File saved snd closed successfully
1.Get data from mobile no
2.get data from name
Enter choice 1
Enter the mobile no which you want to search: 123456789
File contents..
name: Pratiksha
Mo_no:
123456789You want to serch more data ? y
1.Get data from mobile no
2.get data from name
Enter choice 2
Enter the name which you want to search: Ankita
File contents..
name: Ankita
Mo_no:
98765432You want to serch more data
Hope this helps you!!! ❤❤
Answer:
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
#include<string.h>
class Person
{
public:
char name[10];
int PhNo;
void input_data()
{
cout<<"Enter the Name:";
cin>> name;
cout<<"Enter the PhNo:";
cin>>PhNo;
}
void put_data()
{
cout<<setw(10)<<name<<setw(10)<<PhNo<<endl;
}
};
int main()
{
Person rec;
int Phone,pos,choice,offset,i;
fstream fp;
ifstream in;
ofstream out;
char nm[20];
char ans;
do
{
cout<<"\n1.Read file";
cout<<"\n2.Write file";
cout<<"\n3.Determine Name if telephone number is specified";
cout<<"\n4.Determine telephone if name is specifeied";
cout<<"\n5.Update telephone number";
cout<<"\nEnter the choice:";
cin>>choice;
switch(choice)
{
case 1:
in.open("test.txt",ios::in|ios::binary);
cout<<"\nThe contents of file are:\n";
while(in.read((char*)&rec,sizeof(rec)))
{
rec.put_data();
}
in.close();
break;
case 2:
rec.input_data();
char ch;
cin.get(ch);
out.open("test.txt",ios::out|ios::app|ios::binary);
out.write((char*)&rec,sizeof(rec));
out.close();
break;
case 3:
cout<<"\nEnter the phone No:";
cin>>Phone;
fp.open("test.txt",ios::ate|ios::in|ios::out|ios::binary);
fp.seekg(0,ios::beg);
pos=-1;
i=0;
while(fp.read((char*)&rec,sizeof(rec)))
{
if(Phone == rec.PhNo)
{
pos = i;
break;
}
i++;
}
offset=pos*sizeof(rec);
fp.seekp(offset);
fp.read((char*)&rec,sizeof(rec));
cout<<"\nName:"<<rec.name;
fp.close();
break;
case 4:
cout<<"\nEnter the Name:";
cin>>nm;
fp.open("test.txt",ios::ate|ios::in|ios::out|ios::binary);
fp.seekg(0, ios::beg);
pos = -1;
i = 0;
while(fp.read((char*)&rec,sizeof(rec)))
{
if((strcmp(nm,rec.name))==0)
{
pos = i;
break;
}
i++;
}
offset = pos*sizeof(rec);
fp.seekp(offset);
fp.read((char*)&rec, sizeof(rec));
cout<<"\nTelephone Number:"<<rec.PhNo;
fp.close();
break;
case 5:
cout<<"\nEnter the Name:";
cin>>nm;
fp.open("test.txt",ios::ate|ios::in|ios::out|ios::binary);
fp.seekg(0,ios::beg);
pos = -1;
i=0;
while(fp.read((char*)&rec,sizeof(rec)))
{
if((strcmp(nm,rec.name)) == 0)
{
pos = i;
break;
}
i++;
}
offset = (pos)*sizeof(rec);
fp.seekp(offset);
cout<<"\nCureent Phone :"<<rec.PhNo;
cout<<"\nEnter new telephoine Number:";
cin>>Phone;
rec.PhNo=Phone;
fp.write((char*)&rec,sizeof(rec))<<flush;
cout<<"\nrecord updated !!\n";
fp.seekg(0);
while(fp.read((char*)&rec,sizeof(rec)))
{
rec.put_data();
}
fp.close();
break;
}
cout<<"\n Do You want to continue?(y/n)";
cin>>ans;
}while(ans=='y');
return 0;
}
#SPJ2