A file contains a
list of names and telephone numbers in the following form:
Name Tel. No.
Write a C++ program to read the file and output the list in the tabular
format. The name should be left-justified and numbers right-justified. Use a
class object to store each set of data.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<fstream.h>
class phone
{
public:
char name[20]; long
int phone_no; void getdata()
{
cout<<"Enter
the name and phone_no"<<"\n"; cin>>name>>phone_no;
}
void putdata()
{
cout<<endl<<setw(20)<<setiosflags(ios::left)<<name<<setw(10)
<<setiosflags(ios::right)<<phone_no<<endl;
}
};
void main()
{
fstream f; phone ph; int n;
f.open("phonefile",ios::out|ios::in|ios::app);
clrscr();
cout<<"\nEnter the total no_of records:";
cin>>n;
for(int i=0;i<n;i++)
{
ph.getdata();
f.write((char *)&ph,sizeof(ph));
}
f.seekg(0);
cout<<"\n\nThe
content of the file is:\n\n"; cout<<setw(20)<<setiosflags(ios::left)<<"NAME"
<<setw(10)<<setiosflags(ios::right)<<"TELEPHONE_NO";
while(f.read((char *)&ph,sizeof(ph)))
ph.putdata();
f.close();
getch();
}
**********************************OUTPUT********************************
Enter the total no_of records:3 Enter the name and phone_no RAGHU
2456570
Enter the name and phone_no RAJEEV
2457859
Enter the name and phone_no RANJU
2451230
The content of the
file is:
NAME TELEPHONE_NO
RAGHU 2456570
RAJEEV 2457859
RANJU 2451230