Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Cmptr ass
1. /*Q.12 Given a binary file PHONE.DAT, containing records of the following structure type-:
class phonlist
{
char Name[20] ;
char Address[30] ;
char AreaCode[5] ;
char phoneNo[15] ;
public :
void Register();
void show();
};
Write a function TRANSFER() in C++, that would copy all those records which are having
AreaCode as "DEL" from PHONE.DAT to PHONBACK.DAT. */
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
#include<string.h>
class phonelist
{
char name[20] ;
char address[30] ;
char areacode[5] ;
char phoneno[15] ;
public :
void reg()
{
cout<<"enter name : ";
gets(name);
cout<<"enter address : ";
gets(address);
cout<<"enter areacode : ";
gets(areacode);
cout<<"enter phone number : ";
gets(phoneno);
}
void show()
{
2. cout<<"enter name : ";
cout<<"enter address : ";
cout<<"enter areacode : ";
cout<<"enter phone number : ";
}
char* return_code()
{
return areacode;
}
}p[5] ;
void main()
{
phonelist temp;
int n,i;
cout<<"for how many people you wish to enter the details ";
cin>>n;
ofstream fout;
fout.open("phone.dat",ios::binary);
//writing in phone.dat
for(i=0;i<n;i++)
{
p[i].reg();
fout.write((char*)&p[i],sizeof(phonelist));
}
ifstream fin;
fin.open("phone.dat",ios::binary);
fin.open("phone.dat",ios::binary);
fout.open("phoneback.dat",ios::binary);
//transferring records with del areacode to phoneback.dat
3. i=0;
while(fin)
{
fin.read((char*)&temp,sizeof(phonelist));
if(strcmp("del",p[i].return_code())==0)
{
fout.write((char*)&temp,sizeof(phonelist));
i++;
}
}
//displaying the records in phoneback.dat
fin.open("phoneback.dat",ios::binary);
while(fin)
{
fin.read((char*)&temp,sizeof(phonelist));
temp.show();
}
fin.close();
fout.close();
getch();
}
/*Q.11 Write a program in C++ to transfer a particular type of stock from the file "stock.dat" to
another file "Site.dat". Assuming that the binary file is containing the records of following
structure:
struct Products
{
int id;
char Iname[30];
int type;
};
Remember that transfer means deletion from the "stock.dat" file and addition in the "site.dat"
file.
4. */
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
struct products
{
int id;
char iname[30];
int type;
};
void main()
{
products p[5],temp;
int n,t,i;
cout<<"for how many products you wish to enter information ";
cin>>n;
ofstream fout;
fout.open("stock.dat",ios::binary);
//entering the details in stock file
for(i=0;i<n;i++)
{
cout<<"enter id : ";
cin>>p[i].id;
cout<<"enter name : ";
gets(p[i].iname);
5. cout<<"enter type : ";
cin>>p[i].type;
fout.write((char*)&p[i],sizeof(products));
}
fout.close();
cout<<"which type of stock you wish to transfer to other file ";
cin>>t;
ifstream fin;
fin.open("stock.dat",ios::binary);
fout.open("site.dat",ios::binary);
i=0;
//loop to transfer the specified records in site file
while(fin)
{
fin.read((char*)&temp,sizeof(products));
if(p[i].type==t)
fout.write((char*)&temp,sizeof(products));
i++;
}
fin.close();
fout.close();
remove("stock.dat");
fin.open("site.dat",ios::binary);
6. while(fin)
{
fin.read((char*)&temp,sizeof(products));
cout<<" id : "<<temp.id;
cout<<"n name : "<<temp.iname;
cout<<"n type : "<<temp.type;
}
fin.close();
getch();
}