Friday

Important Questions for Class 12 Computer Science (C++) – Data File Handling (Part 2)

 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. Delhi 2013C

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 GetCommO {return Commission;} 
long GetAcodeO {return ACode;}
};
void ChangeCommissiondong 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 ();
}

Answer:
Statement 1

F.seekp(-1*sizeof(A),ios::cur)

Statement 2

F.write((char*)&A,sizeof(A))

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. Delhi 2012C

#include<fstream.h>
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;
whileUFound && 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();
}

Answer:
Statement 1

File.seekpCRec,ios: :beg)

Statement 2

File.write((char*)&M,sizeof(M))

Question 13:
Observe the program segment given below carefully and answer the questions that follow: All India 2012

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 GetlnoO {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.

Answer:
Statement 1

File.seekp(-1*sizeof(S),ios::cur);

Statement 2

File.write((char*)&S,sizeof(S));

Question 14:
Observe the program segment given below carefully and answer the questions that follow: Delhi 2012

class Inventory 
{
int Ano, Qty; char Article[20]; 
public:
void Input!()
{
cin>>Ano; 
gets(Article); 
cin>>Qty;
}
void Issue(int Q)HQty 4= 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++;
}
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.

Answer:
Statement 1

File.seekp(-1*sizeof(I),ios::cur);

Statement 2

File.write((char*)&I,sizeof(I));

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. Delhi 2011

#include<fstream.h>
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.writet(char*)this,sizeof(product));
}
}
fil.close();
}

Answer:
Statement 1

fil.tellg( )-sizeof(product)

Statement 2

fil.seekp(Position) 

or

fil.seekp(-1*sizeof(product),ios::cur)

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. Delhi 2011C

#include<fstream.h> 
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;
Fi le.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();
}
}

Answer:
Statement 1

if(C.Code == Record);

Statement 2

File.seekp(-1*sizeof (C), ios:: cur);

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

#include<fstream.h>
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();
}

Answer:
Statement 1

File.tellg()-sizeof (ITEM)

Statement 2

File.seekp(FilePos,ios::beg) 
or
File.seekp(-1*sizeof(ITEM),ios::cur)

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. All India 2010

#include<fstream.h> 
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();
}

Answer:
Statement 1

F.tellg():

Statement 2

F.seekp(Pos-sizeof(C),ios::beg): 
or
F.seekp(-1*sizeof(C),ios::cur);

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. Delhi 2010

#include<fstream.h> 
class Client 
{
long Cno;
char Name[20], Email[30]; 
public:
void Enter(); //Function to allow user to enter the Cno.Name,Email 
void ModifyO; //Function to allow user to enter(modify) Email 
long ReturnCnoO (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();
}

Answer:
Statement 1

F.tellg():

Statement 2

F.seekp(Pos-sizeof(C),ios::beg);
or
F.seekp(-1*sizeof(C) ,ios: :cur);

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. All India 2009

#include<fstream.h> 
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 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:
whileCPosition <<** - 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();
}
}

Answer:
Line 1

File.seekp(Position)

Line 2

File,write((char*)&L,sizeof(L))

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. Delhi 2009C

#include<fstream.h> 
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;
}

Answer:
Statement 1

File.seekg ((N -1)*sizeof (File_0bject));

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. Delhi 2009

#include<fstream.h> 
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(P o s == -1)
cout<<”No updation done as required Ino Not found:";
File.close();
}

Answer:
Line 1

File.seekp(Pos);

Line 2

File.write((char*)&S, sizeof(S));

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.

Answer:

void PURETEXTO 
{
fstream fp1;
fp1. open("MYNOTES.txt",ios::in |ios::out);
if(!fpl1)
{
cout<<"Cannot open file"<<endl;
}
char ch; 
char c;
whi1e(!fpl.eof())
{
c=fp1.get(); 
if(c=='K')
{
fpl.seekg(-1, ios::cur); 
fpl.put('C');
}
}
fp1.clear();
fp1.seekp(0,ios::beg); 
cout<<"\n After replacing character\n";
while(!fp1.eof())
{
fp1.get(ch); 
cout<<ch;
}
fp1.close();
}

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;}
};

Answer:

void COUNTPICS( )
{
int count = 0:
PHOTOS obj;
ifstream fp1;
fp1.open("PHOTOS.DAT", ios::binary);
while (fp1.read((char*)&obj, sizeof(obj)))
{
if(strcmpi(obj.GETPTYPE!), "PORTRAIT")==0)
count++;
}
cout<<"The total number of PHOTOS of type PORTRAIT is "<<count; 
fp1.close();
}

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: Delhi 2016

“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”

Answer:

void W0RD4CHAR()
{
ifstream fin("FUN.TXT”); 
char word[80]; 
while(!fin.eof())
{
fin>>word;
if(strlen(word)==4&& 
(!(word[3]<65| | (word[3]>90&& 
word[3]<97)| | word[3]<122))) 
cout<<word<<"";
else if(strlen(word)==5&&((word[4] 
<65| |word[4]>90)&&(word[4]<97| | 
word[4]<122)))
{
for(inti=0;i<4;i++) 
cout<<word[i]; 
cout<<"";
}
}
fin.close();
}


0 comments:

Post a Comment

Thanks for leaving a comment. As soon as it's approved, it will appear.