Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

OOPs and File Handling Part 1

Question 1:
Find the output of the following C++ code considering that the binary file CLIENTS.DAT exists
on the hard disk with a data of 200 clients:

class CLIENTS
{
int CCode;
char CName[20];
public:
void REGISTER();
void DISPLAY();
};
void main()
{
fstream File;
File.open("CLIENTS.DAT",ios::binary|ios::in);
CLIENTS C;
File.seekg(6*sizeof(C));
File.read((char*)&C, sizeof(C));
cout<<"Client Number:"<<File.tellg()/sizeof(C)+1;
File.seekg(0,ios::end);
cout«"of"<<File.tellg()/sizeof(C)<<endl;
File.close();
}

Question 2:
Find the output of the following C++ code considering that the binary file MEM.DAT exists on the
hard disk with a data of 1000 members:
class MEMBER
{
int Mcode;
char MName[20];
public:
void Register();
void Display();
};
void main()
{
fstream MFile;
MFile.open("MEM.DAT", ios::binary|ios::in);
MEMBER M;
MFile.read((char*) &M. sizeof(M));
cout<<"Rec :"<<MFile.tellg()/sizeof(M)<< endl;
MFile.read((char*) &M, sizeof(M));
MFile.read((char*) &M, sizeof(M));
cout<<"Rec :"<<MFile.tellg()/sizeof(M)<<endl;
MFile.close();
}

Question 3:
Find the output of the following C++ code considering that the binary file CLIENT.DAT exists on
the hard disk with a data of 1000 clients:

class CLIENT
{
int Ccode;
char CName[20];
public:
void Register();
void Display();
};
void main()
{
fstream CFile;
CFile.open("CLIENT.DAT",ios::binary|ios::in);
CLIENT C;
CFile.read((char*)&C, sizeof(C));
cout<<"Rec: "<<CFile.tellg()/sizeof(C)<<endl;
CFile.read((char*)&C, sizeof(C));
CFile.read((char*)&C, sizeof(C));
cout<<"Rec:"<<CFile.tellg()/sizeof(C)<<endl;
CFile.close();
}
Question 4:
Find the output of the following C++ code considering that the binary file CLIENT.DAT exists on
the hard disk with records of 100 members.

class CLIENTS
{
int Cno;
char Name[20];
public:
void In();
void Out();
};
void main()
{
fstream CF;
CF.open("CLIENTS.DAT", ios::binary|ios: :in);
CLIENTS C;
CF.read((char*)&C,sizeof(C));
CF.read((char*)&C,sizeof(C));
CF.read((char*)&C,sizeof(C));
int POS=CF.tellg()/sizeof(C);
cout<<"PRESENT RECORD: "<<POS<<endl;
CF.close();
}

Question 5:
Find the output of the following C++ code considering that the binary file MEMBER.DAT exists on
the hard disk with records of 100 members.
class MEMBER
{
int Mno;
char Name[20];
public:
void In();
void Out(); 
};
void main()
{
fstream MF;
MF.open("MEMBER.DAT", ios::binary|ios::in);
MEMBER M;
MF.read((char*)&M. sizeof(M));
MF.read((char*)&M, sizeof(M));
MF.read((char*)&M, sizeof(M));
int P0SITI0N=MF.tellg()/sizeof(M);
cout<<"PRESENT RECORD: "<<P0SITI0N<<endl ;
MF.close();
}

Question 6:
Find the output of the following C++ code considering that the binary file GAME. DAT exist on
the hard disk with information of around 200 games.

class GAME
{
int Gno;
char GName[20];
public:
void Getln();
void ShowName();
};
void main()
{
fstream GF;
GF.open("GAME.DAT",ios::binary | ios::in);
GAME G;
GF.seekg(sizeof(G)*5);
GF.read((char*)&G, sizeof(G)):
GF.read((char*)&G, sizeof(G));
int REC0RDN0=GF.tellg()/sizeof(G);
cout<<"REC0RDN0: "<<REC0RDN0<<endl;
GF.close();
}
Question 7:
Fill in the blanks marked as Statement 1 and Statement 2 in the program segment given below
with appropriate functions for the required task.

class Medical
{
int RNo; //Representative Code
char Name[20]; //Representative Name
char Mobile[12]; //Representative Mobile
public:
void Input(); //Function to enter all details
void Show(); //Function to display all details
int RRno()
{
return RNo;
}
void ChangeMobile() //Function to change Mobile
{
cout<<"Changed Mobile:";
gets(Mobile);
}
};
void RepUpdate()
{
fstream F;
F.open("REP.DAT", ios::binary|ios::in|ios::out);
int Change=0;
int URno;
cout<<"Rno(Rep No-to update Mobile):";
cin>>URno;
Medical M;
while(!Change && F.read((char*)&M,sizeof(M)))
{
if(M.RRno() == URno)
{
//Statement l:To call the function to change Mobile No.;
//Statement 2:To reposition file pointer to re-write
//The updated object back in the file ;
F.write!(char*)&M,sizeof(M)); Change++;
}
}
if(Change)
cout<<"Mobile Changed for Rep"<<URno<<endl;
else
cout<<"Rep not in the Medical "<<endl;
F.close();
}
Question 8:

Fill in the blanks marked as Statement 1 and Statement 2 in the program segment given below
with appropriate functions for the required task.

class Agency
{
int ANo; //Agent code
char AName[20]; //Agent name
char Mobile[30]; //Agent mobile
public:
void Enter(); //Function to enter details of agent
void Disp(); //Function to display details of agent
int RAno()
{
return ANo;
}
void UpdateMobileO //Function to update Mobile
{
cout<<"Updated Mobile:"; gets(Mobile);
}
};
void AgentUpdate()
{
fstream F;
F.open("AGENT.DAT",ios::binary|ios::in|ios::out);
int Updt=0;
int UAno;
cout<<"Ano (Agent No-to update Mobile):";
cin>>UAno; Agency A;
while(Updt && F.read((char*)&A,sizeof(A)))
{
if (A.RAnoO == UAno)
{
//Statement 1:To call the function to Update Mobile No;
//Statement 2:To reposition file pointer to re-write
//The updated object back in the file;
F.write((char*)&A,sizeof(A)); Updt++;
}
}
if(Updt)
cout<<"Mobile Updated for Agent"<<UAno<<endl;
else
cout<<"Agent not in the Agency"<<endl;
F.close();
}
Question 9:
Fill in the blanks.marked as Statement 1 and Statement 2 in the program segment given below
with appropriate functions for the required task.
class Club
{
long int MNo;//Member number
char MName[20];//Member name
char Email[30];//Email of member
public:
void Register();//Function to register member
void Disp();//Function to display details
void ChangeEmail()//Function to change Email
{
cout<<"Enter Changed Email:";
cin>>Email;
}
long int GetMno()
{
return MNo;
}
};
void ModifyData()
{
fstream File;
File.open("CLUB.DAT",ios::binary|ios::in|ios::out);
int Modify = 0, Position;
long int ModiMno;
cout<<"Mno-whose email required to be modified:";
cin>>ModiMno;
Club CL;
while(!Modify&&Fi1e.read((char*)&CL,sizeof(CL)))
{
if(CL.GetMno() == ModiMno)
{
CL.ChangeEmail();
Position = File.tellg()/sizeof(CL);
//Statement 1:To place file pointer to the required position
//Statement 2:To write the object CL onto the binary file
Modify++;
}
}
if(Modify)
cout<<"Email Changed..."<<endl;
else
cout<<"Member not found.. ."<<endl;
File.close();
}

Question 10:
Fill in the blanks marked as Statement 1 and Statement 2 in the program segment given below
with appropriate functions for the required task.

class Customer
{
long int CNo; //Customer number
char CName[20]; //Customer name
char Email[30]; //Email of customer
public:
void Allocate(); //Function to allocate member
void Show(); //Function to show customer data
void ModifyEmail() //Function to modify Email
{
cout<<"Enter Modified Email:";
gets(Email);
}
long int GetCno()
{
return CNo;
}
};
void ChangeData()
{
fstream File;
File.open("CUST.DAT", ios::binary|ios::in|ios::out);
int Change = 0, Location;
long int ChangeCno;
cout<<"Cno-whose Email required to be modified:";
cin>>ChangeCno;
Customer CU;
while(!Change && File.read((char*)&CU,sizeof(CU)))
{
if(CU.GetCno() = ChangeCno)
{
CU.ModifyEmai1();
Location = File.tellg()==sizeof(CU);
//Statement 1:To place file pointer to the required position
//Statement 2:To write the object CU on to the binary file
Change++;
}
}
if(Change)
cout<<"Email Modified..."<<endl;
else
cout<<"Customer not found..."<<endl;
File.close();
}
Question 11:
Fill in the blanks marked as Statement 1 and Statement 2 in the program segment given below
with appropriate functions for the required task. 
class Agent
{
long ACode;
//Agent Code
char AName [20]; //Agent Name
int Commission;
public:
void Enter();
//Function to enter details of Agent
void Display();
//Function to display details of Agent
void Update(int C)
//Function to modify commission
{
Commission = C;
}
int GetComm()
{
return Commission;
}
long GetAcode()
{
return ACode;
}
};
void ChangeCommission(int AC, int CM)
//AC -> Agent Code, whose commission needs to change
//CM -> New commission
{
fstream F;
F.open("AGENT.DAT",ios::binary|ios::in|ios::out);
char Changed = " N";
Agent A;
while(Changed == "N" && F.read((char*)&A,sizeof(A)))
{
if(A.GetAcode()==AC)
{
Changed = "Y";
A.Update(CM);
//Statement l:To place file pointer to the required position
___________________________________;
//Statement 2:To write the object A onto the binary file
___________________________________;
}
}
if(Changed=="N")
cout <<"Agent not registered...";
F.close ();
}

Question 12:
Fill in the blanks marked as Statement 1 and Statement 2 in the program segment given below
with appropriate functions for the required task. 
#include<fstream>
using namespace std;
class Movie
{
long MNo; //Movie Hall Number
char MName[20]; //Movie Name
int Seats; //Number of Vacant Seats
public:
void MovieIn();
void MovieOut();
void Booking(int N) //Function to Book Seats
{
Seats -=N;
}
int RSeats()
{
return Seats;
}
long RMno()
{
return MNo;
}
};
void BookMySeat(long MH,int S)
//MH stands for Hall Number
//S stands for Number of Tickets to purchase
{
fstream File;
File.open("M0VIE.DAT", ios::binary|ios::in|ios::out);
int Found = 0, Booked = 0, Rec;
Movie M;
while(Found && File.read((char*)&M,sizeof(M)))
{
if(M.RMnoC)==MH)
{
Found = 1;
if((M.RSeats()-S)> 0)
{
M.Booking(S);
Booked = 1;
}
else
cout<<"House Full";
}
}
if(Found && Booked)
{
Rec = File.tellg()/sizeof(M);
//Statement 1 : To place file pointer to the required position
//Statement 2 : To write the object M on to the binary file
}
if(! Found)
cout<<"No updation done as Hall not found.";
File.close();
}

Question 13:
Observe the program segment given below carefully and answer the questions that follow:
class Stock
{
int Ino, Qty;
char Item[20];
public:
void Enter()
{
cin>>Ino;
gets(Item);
cin>>Qty;
}
void Issue(int Q)
{
Qty += Q;
}
void Purchase(int Q)
{
Qty-= Q;
}
int GetIno()
{
return Ino;
}
};
void Purchaseltem(int Pino,int PQty)
{
fstream File;
File.open("STOCK.DAT", ios::binary|ios::in|ios::out);
Stock S;
int Success = 0;
while(Success == 0 && File.read((char*)&S,sizeof(S)))
{
if (Pino == S.Getlno())
{
S.Purchase(PQty);
//Statement 1
//Statement 2
Success++;
}
}
if(Success == 1)
cout<<"Purchase Updated"<<endl;
else
cout<<"Wrong item No."<<endl;
File.closeO;
}
(i) Write Statement 1 to position the file pointer to the appropriate place, so that the data
updation is done for the required item.
(ii) Write Statement 2 to perform the write operation, so that updation is done in the binary file.

Question 14:
Observe the program segment given below carefully and answer the questions that follow:
class Inventory
{
int Ano, Qty;
char Article[20];
public:
void Input()
{
cin>>Ano;
gets(Article);
cin>>Qty;
}
void Issue(int Q)
{
Qty += Q;
}
void Procure(int Q)
{
Qty -= Q;
}
int GetAno()
{
return Ano;
}
};
void ProcureArticle(int TAno, int TQty)
{
fstream File;
File.open("ST0CK.DAT", ios::binary|ios::in|ios::out);
Inventory I;
int Found = 0;
while(Found == 0 && File.read((char*)&I.sizeof(I)))
{
if (TAno == I.GetAno())
{
I.Procure(TQty);
//Statement 1
//Statement 2
Found++;
break;
}
}
if (Found == 1)
cout<<"Procurement Updated"<<endl;
else
cout<<"Wrong Article No"<<endl;
File.close();
}
(i) Write Statement 1 to position the file pointer to the appropriate place, so that the data
updation is done for the required Article.
(ii) Write Statement 2 to perform the write operation, so that the updation is done in the binary
file.

Question 15:
Observe the program segment given below carefully and fill in the blanks marked as Statement 1
and Statement 2 using seekg() or seekp() or tellg() or tellp() functions for performing the
required task.

#include<fstream>
using namespace std;
class product
{
int pno;
char pname[20];
int qty;
public:
void modifyqty(); //Function is to modify quantity of product
};
void product::modifyqty()
{
fstream fil;
file.open("product.dat",ios::binary|ios::in|ios::out);
int mpno;
cout<<"product number to modify quantity ";
cin>>mpno;
while(fil.read((char*)this.sizeof(product)))
{
if(mpno == pno)
{
cout<<"present quantity:"<<qty<<endl;
cout<<"changed quantity:";
cin>>qty;
int position =__________; //Statement 1
___________________; //Statement 2
fil.write((char*)this,sizeof(product));
}
}
fil.close();
}

Question 16:
Observe the program segment given below carefully and fill in the blanks marked as
Statement 1 and Statement 2. You can use any function from seekg() , seekp() , tellp()
and tellg() for performing the required task.
#include<fstream>
using namespace std;
class Country
{
int Code;
char Name[20];
int Population;
public:
//Function to search and display the content from a particular record
number
void SearchFor(int);
//Function to modify the content of a particular record number
void Update(int);
};
void Country::SearchFor(int Record)
{
Country C;
fstream File;
File.open("COUNTRY.DAT",ios::binary|ios::in);
File.read((char*)&C,sizeof(C));
_________ //Statement1
cout<<C.Code<<"==>"<<C.Name<<"==>"<<C.Population<<endl;
File.closet();
}
void Country::Update(int Record)
{
Country C;
fstream File;
File.open("COUNTRY.DAT",ios::binary |ios::in|ios::out);
cin>>C.Code;
cin.getline(C.Name,20);
cin>>C.Population;
_________ //Statement 2
File.write((char*)&C,sizeof(C));
File.closet();
}

Question 17:
#include<fstream>
using namespace std;
class ITEM
{
int Ino;
char Iname[20];
float Price;
public:
void ModifyPrice();
//The function to modify price of a particular ITEM
};
void ITEM::ModifyPrice()
{
fstream File;
File.open("ITEM.DAT",ios::binary|ios::in|ios::out);
int Cino;
cout<<"Item No to modify price:";
cin>>Cino;
while(File.read((char*)this,sizeof(ITEM)))
{
if(Cino == Ino)
{
cout<<"Present Price:"<<Price<<endl;
cout<<"Changed Price:";
cin>>Price;
int FilePos =___________;
//Statement 1 __________________;
//Statement 2
File.write((char*)this,sizeof(ITEM));
//Re-writing the record
}
}
File.close();
}

Question 18:
Observe the program given below carefully and fill in the blanks marked as Statement 1 and
Statement 2 using tellg() and seekp() functions for performing the required task.
#include<fstream>
using namespace std;
class Customer
{
long Cno;
char Name[20], Mobi1e[12];
public:
//function to allow user to enter the Cno, Name , Mobile
void Enter();
//function to allow user to enter (modify) Mobile number
void Modify();
//function to return value of Cno
long GetCno()
{
return Cno;
}
};
void ChangeMobile()
{
Customer C;
fstream F;
F.open("CONTACT.DAT". ios::binary|ios::in|ios::out);
long Cnoc; //Customer number whose mobile number needs to be changed
cin>>Cnoc;
while(F.read((char*)&C,sizeof(C)))
{
if(Cnoc == C.GetCno())
C.Modify();
//Statement 1 to find the current position of file pointer
int Pos = _________________;
//Statement 2 to move the file pointer to write the
//modified record back onto the file for desired Cnoc
F.write((char*)&C,sizeof(C));
}
F.close();
}

Question 19:
Observe the program segment given below carefully and fill in the blanks marked as Statement 1
and Statement 2 using tellg() and seekp() functions for performing the required task.
#include<fstream>
using namespace std;
class Client
{
long Cno;
char Name[20], Email[30];
public:
void Enter();
//Function to allow user to enter the Cno.Name,Email
void Modify(); //Function to allow user to enter(modify) Email
long ReturnCno()
{
return Cno;
}
};
void ChangeEmail()
{
Client C;
fstream F;
F.open("INF0.DAT",ios::binary|ios::in|ios::out);
long Cnoc;
//Client’s number whose Email needs to be changed
cin>>Cnoc;
while(F.read((char*)&C,sizeof(C)))
{
if(Cnoc == C.ReturnCno())
{
C.Modify();
//Statement 1 to find the current position of file pointer
int Pos = ______________
//Statement 2 to move the file pointer to write the
//modified record back onto the file for the desired Cnoc
______________
F.write((char*)&C,sizeof(C));
}
}
F.close();
}

Question 20:
Observe the program segment given below carefully and fill in the blanks marked as Line 1 and
Line 2 using fstream function for performing the required task.
#include<fstream>
using namespace std;
class Library
{
long Ano;
char Title[20];
int Qty;
public:
void Enter(int):
//Function to allow user to enter the data
void Display();
//Function to display the content
void Buy(int Tqty)
//Function to increment qty
{
Qty += Tqty;
}
long GetAno()
{
return Ano;
}
};
void BuyBookdong(int BAno,int BQty)
//BAno : Ano of number of book purchased
//QBty : Number of books purchased
{
Library L;
fstream File;
File.open("STOCK.DAT".ios::bina ry|ios::in|ios::out);
int Position = -1;
while(Position < - 1 && File.read((char*)&L,sizeof(L)))
{
if(L.GetAno( ) == BAno)
{
L.Buy(BQty); //To update the number of books
Position = File.tellg()/sizeof(L):\;
_______________________;
//Line 1 : to place the file pointer to the required position
_______________________;
//Line 2 : to write the object L on to the binary file
}
if(Position == -1)
cout<<"No updation done as required Ano not found:";
File.close();
}
}
Question 21:
Observe the program segment given below carefully and fill in the blank marked as Statement 1
using seekg() or seekp() functions for performing the required task.

#include<fstream>
using namespace std;
class File_Object
{
int No;
char Name[20];
public:
//Function to read Nth record from the file
void Goto_Record(int N);
};
void File_Object::Goto_Record(int N)
{
fstream File;
File_Object Record;
File.open("STOCK.DAT".ios::binary|ios::in);
____________ //Statement 1
File.read((char*)&Record,sizeof(Record));
cout<<Record.No<<Record.Name<<endl;
}

Question 22:
Observe the program segment given below carefully and fill in the blanks marked as Line 1 and
Line 2 using fstream function for performing the required task.
#include<fstream> using namespace std;
class Stock
{
long Ino;
char ITem[20];
int Qty;
public:
void Get(int);
//Function to allow user to enter the Ino, Item, Qty
voidshow();
//Function to display the content
void Purchase(int Tqty)
//Function to increment qty
{
Qty += Tqty;
}
long KnowIno()
{
return Ino;
}
void Purchaseitem(long PIno, int Pqty)
//Pino : Ino of item purchased
//Pqty : Number of item purchased
{
Stock S;
fstream File;
File.open("ITEMS.DAT",ios:;binary|ios::in|ios:;out);
int Pos = -1;
while (Pos == -1 && File.read((char*)&S,sizeof(S)))
{
if(S.KnowIno() == Pino)
{
S.Purchase(Pqty);
//to update the number of items
Pos = File.tellg()/sizeof(S);
_____________________
/*Line 1: to place the file pointer
to the required position*/
_____________________
/*Line 2; to write the object S
on to the binary file*/
}
}
if(Pos == -1)
cout<<"No updation done as required Ino Not found:";
File.close();
}
};

Question 23:
Polina Raj has used a text editing software to type some text in an article. After saving the article
as MYNOTES.TXT , she realised that she has wrongly typed alphabet K in place of alphabet C
everywhere in the article.
Write a function definition for PURETEXT() in C++ that would display the corrected version of
the entire article of the file MYNOTES. TXT with all the alphabets “K” to be displayed as an
alphabet “C” on screen.
NOTE Assuming that MYNOTES. TXT does not contain any C alphabet otherwise.
Example:
If Polina has stored the following content in the file MYNOTES.TXT :

I OWN A KUTE LITTLE KAR. I KARE FOR IT AS MY KHILD.

The function PURETEXT() should display the following content:

I OWN A CUTE LITTLE CAR. I CARE FOR IT AS MY CHILD.

Question 24:
Write a definition for function COUNTPICS() in C++ to read each object of a binary file
PHOTOS.DAT , find and display the total number of PHOTOS of type PORTRAIT. Assume that the
file PHOTOS.DAT is created with the help of objects of class PHOTOS , which is defined below:
class PHOTOS
{
int PCODE;
char PTYPE[20];
//Photo Type as "PORTRAIT", "NATURE"
public:
void ENTER()
{
cin>>PCODE;
gets(PTYPE);
}
void SHOWCASE()
{
cout<<PC0DE<<": "<<PTYPE<<endl;
}
char *GETPTYPE()
{
return PTYPE;
}
};

Question 25:
Write function definition fof WORD4CHAR() in C++ to read the content of a text file FUN.TXT
and display all those words, which has four characters in it.
e.g. If the content of the file FUN.TXT is as follows: 

When I was a small child, I used to play in the garden with my grand mom. Those
days were amazingly funful and I remember all the moments of that time

The function WORD4CHAR() should display the following:

When used play with days were that time

Question 26:
Write a definition for function BUMPER() in C++ to read each object of a binary file GIFTS.DAT ,
find and display details of those gifts, which has remarks as “ON,DISCOUNT”. Assume that the
file GIFTS.DAT is created with the help of objects of class GIFTS , which is defined below:
class GIFTS
{
int ID;
char Gift[20], Remarks[20];
float Price;
public:
void Takeonstock()
{
cin>>ID;
gets(Gift);
gets(Remarks);
cin>>Price;
}
void See()
{
cout<<ID<<":"«Gift<<":" <<Price<< ": "<<Remarks<<endl;
}
char *GetRemarks()
{
return Remarks;
}
};

Question 27:
Write function definition of DISP3CHAR() in C++ to read the content of a text file KIDINME.TXT
and display all those words, which has three characters in it. e.g. If the content of the file
KIDINME.TXT is as follows:

When I was a small child, I used to play in the garden with my grand mom.
Those days were amazingly funful and I remember all the moments of that time.

The function DISP3CHAR() should display the following:

was the mom and all the

Question 28:
Write a definition for function ONOFFER() in C++ to read each object of a binary file
TOYS.DAT, find and display details of those toys, which has status as “ON OFFER”. Assume that
the file TOYS.DAT is created with the help of objects of class TOYS, which is defined below:
class TOYS
{
int TID;
char Toy[20], Status[20];
float MRP;
public:
void Getinstock()
{
cin>>TID;
gets(Toy);
gets(Status);
cin>>MRP;
}
voidView()
{
cout<<TID<<": ”<<Toy<<": "<<MRP<<n": "<<Status<<endl;
}
char*SeeOffer()
{
return Status;
}
};

Question 29:
Write function definition for WORDABSTART() in C++ to read the content of a text file JOY.TXT ,
and display all those words, which are starting with either ‘A’, ‘a’ or ‘B’, ‘b’. e.g. If the content of
the file JOY.TXT is as follows:

I love to eat apples and bananas. I was travelling to Ahmedabad to buy some
clothes.

The function WORDABSTART() should display the following:

apples and bananas Ahmedabad buy

Question 30:
Write function definition for SUCCESS() in C++ to read the content of a text file STORY.TXT ,
count the presence of word Success and display the number of occurrence of this word.
NOTE -The word Success should be an independent word
-Ignore type cases (i.e. lower/upper case)
e.g. if the content of the file STORY.TXT is as follows:
Success shows others that we can do it. It is possible to achieve success with
hard work. Lot of money does not mean SUCCESS.

The function SUCCESS() should display the following:

You might also like