MENU

Chapter 6 Data File Handling Solutions

Question - 21 : -
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("STOCK.DAT", ios :: binary|ios :: in|ios :: out);
 Inventory I;
 int Found=0;
 while (Found==0 && File, read((char*)&I. sizeof(I)))
 if (TAno=|S.GetAno())
 {
 I. Procure (TQty);
 //Statement 1
 //Statement 2
 Found++;
 }
 }
 if (Found==l)
 {
 cout<<" Procurement Updated" << endl ;
 else
 cout<<"Wrong Article No"<
 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 term.
(ii) Write statement 2 to perform the write operation so that the updation is done in the binary file.

Answer - 21 : -

(i) file.seekp(sizeof (qty), ios :: cur);
(ii) file.write((char*)& |, sizeof(|));

Question - 22 : -
Consider a file F containing objects E of class Emp. 
(i) Write statement to position the file pointer to the end of the file.
(ii) Write statement to return the number of bytes from the begining of the file to the current position of the file pointer.

Answer - 22 : -

(i) F. seekg (0,ios::end);
(ii) F.tellg();

Question - 23 : -
Write a function RevText() to read a text file “Input.txt” and Print only word starting with ‘I’in reverse order. [Delhi, 2015]
Example : If value in text file is : INDIA IS MY COUNTRY v
Output will be : AIDNI SI MY COUNTRY

Answer - 23 : -

void RevText ()
 {ifstream in ("Input.txt");
 char word[25];
 while(in)
 { in >>word;
 if (word[0] = = 'I' )
 cout<
 else
 cout<
 }

Question - 24 : -
Write a function in C++ to search display details, where destination is “Chandigarh” from binary file “Flight.Dat”. Assuming the binary file is containing the objects of the following class :

 class FLIGHT
 { int Fno; // Flight Number
 char From [20]; // Flight Starting Point
 char To [20]; //Flight Destination
 public :
 char * GetFrom (); { return from;}
 char * GetTo(); { return To; }
 void input()
 {cin>>Fno>>; gets(From); get (To);}
 void show()
 {cout<
 };

Answer - 24 : -

void Dispdetails ()
 {ifstream fin ("Flight.Dat");
 Flight F;
 while (fin)
 {fin.read((char*)&F,sizeof(F))
 if (strcmp(F.GetTo0, "Chandigarh"))
 F.show();
 }
 }

Question - 25 : -
Write a definition for function COSTLY() In C++ to read each record of a binary file GIFTS. DAT, find and display those items, which are priced more than 2000. Assume that the file GIFTS.DAT is created with the help of objects of class GIFTS, which is defined below:

class GIFTS
 {
 int CODE; char ITEM [20] ; float PRICE;
 public :
 void procure ()
 {
 cin>>CODE; gets (ITEM); cin>>PRICE;
 }
 void View ()
 {
 cout<
 float GetPrice () {return PRICE;
 }.
Answer:

Answer - 25 : -

void COSTLY ()
 {
 GIFTS G;
 ifstream fin ("GIFTS.DAT" , ios : : binary);
 while (fin.read( (char *)&G, sizeof (G)))
 {
 if (G.GetPrlce () >2000)
 G.View ();
 }
 fin. close ();
OR
Any other correct equivalent function definition

Question - 26 : -
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 POSITION = MF. tellg () / sizeof (M) ;
 Cout<<"PRESENT REOCRD :"< MF.close () ;
 }

Answer - 26 : -

PRESENT RECORD : 3

Question - 27 : -
Write a definition for function Economic ( ) in C++ to read each record of a binary file ITEMS.DAT, find and display those items, which costs less than 2500. Assume that the file ITEMS. DAT is created with the help of objects of class ITEMS, which is defined below :

class ITEMS
 {
 int ID; char GIFT [20] ; float cost ;
 public :
 void Get ()
 {
 cin>>CODE,-gets (GIFT) ;
 cin>>cost;
 }
 void See ()
 {
 COUt < }
 float GetCost 0 {return Cost;}.
 } ;

Answer - 27 : -

void Economic ()
 {
 ITEMS I;
 ifstream fin (" ITEMS.DAT",ios:: binary);
 while (fin.read ((char *)&I,sizeof (I)))
 {
 if (I.GetCost ()<2500)
 I. See ();
 }
 fin. close ();
 }
OR
Any other correct equivalent function definition

Question - 28 : -
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’.
Example:
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 bananas Ahmedabad buy

Answer - 28 : -

void WORDABSTART ()
 {
 ifstream fil; fi1.open ("JOY.TXT");
 char W[20];
 fi1> >W;
 while(!fil.eof())//OR While(fil)
 {
 if(strlen(w)==4) cout< >W;
 }
 fil.close ();//Ignore
 }

Question - 29 : -
Write a definition for function OFFER() in C+ + to read each object of a binary file OFFER.DAT, find and display details of those ITEMS, which has status as “ON OFFER”. Assume that the file OFFER.DAT is created with the help of objects of class ITEMS, which is defined below:

class ITEMS
 {
 int ID;char Item[20];Status[20]; float Price;
 public:
 void GetonstockO
 {
 cin>>ID;gets(Item);gets(Status);
 cin>>Price;
 }
 void view()
 {
 cout<
 char*IStatus(){return Status}.
 };

Answer - 29 : -

void OFFER ()
 {
 ITEMS;
 ifstream fin;
 fin.open("OFFER.DAT",ios::binary);
 while(fin.read(char*)&G size of(G))
 {
 if strcmp (G.Get status(),"ON OFFER")==0)
 G.view();
 }
 fin.close()///Ignore
}

Question - 30 : -
Given the binary file CAR.Dat, containing . records of the following class CAR type:

class CAR
 {
 int C_No;
 char C_Name[20];
 float Mileage;
 public :
 void enter( )
 {
 cin>> C_No ; gets(C_Name) ; cin >>Mileage;
 }
 void display( )
 {
 cout<
 cout<
 }
 int RETURN_Mileage( )
 {
 return Mileage;
 }
 };
Write a function in C++, that would read contents from the file CAR.DAT and display the details of car with mileage between 100 to 150.

Answer - 30 : -

void CARSearch ()
 {
 fstream FIL;
 FIL.open("CAR.DAT" , ios::binary|ios::in);
 CAR C;
 int Found = 0;
 while(FIL.read((char*)&C,sizeof(C)))
 {
 if ((C.RETURN_Mileage( )>100) &&
 (C.RETURN_Mileage( ) < 150) )
 {
 C.display();
 Found++;
 }
 }
 if (Found==0)
 cout<<"Sorry! No car found with mileage between 100 to 150";
 FIL.close () ;
 }

×