MENU

Chapter 6 Data File Handling Solutions

Question - 11 : -
Write a function in C + + to count the number of lines present in a text file “STORY.TXT”.

Answer - 11 : -

void CountLine ()
 {
 ifstream FIL("STORY.TXT");
 int LINES=0;
 char STR[80]; while {FIL.getline(STR, 80);
 LINES++;}
 cout<<"No. of Lines:"<
 f.close();
 }

Question - 12 : -
Write the command to place the file pointer at the 10th and 4th record starting position using seekp() or seekg() command. File object is “file”? and record name is “STUDENT”.

Answer - 12 : -

file.seekp( 9 * sizeof(STUDENT), ios::beg);
file.seekp( 3 * sizeof(STUDENT), ios::beg);

Question - 13 : -
Find the output of the following C++ code considering that the binary file sp.dat already exists on the hard disk with 2 records in it.

class sports
 {
 int id;
 char sname[20];
 char coach[20];
 public:
 void entry();
 void show();
 void writing();
 void reading();
 }s;
 void sports::reading()
 {
 ifstream i;
 i.open("sp.dat");
 while(1)
 {
 i.read((char*)&s.size of (s));
 if(i.eof())
 break;
 else
 cout<<"\n"<
 }
 i.close () ;
 }
 void main()
 {
 s.reading();
 }

Answer - 13 : -

42
84

Question - 14 : -
A binary file “games.dat” contains data of 10 games where each game’s data is an object of the following class :

class game
 {
 int gameno;
 char game_name[20];
 public :
 void enterdetails(){cin>>gameno;
 gets(game_name);}
 void enterdetails()
 {cout<
 };
 With reference to this information, write C++ statement in the blank given below 
to move the file pointer to the end of file, ifstream ifile; game G;
 ifile.open("games.dat",ios::binary| ios::in);
 cout<

Answer - 14 : -

ifile.seekg(0,ios::end);

Question - 15 : -
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 [12]; //Agent Mobile
 public:
 void Enter (); /*Function to
 enter details of agent*/
 void Disp (); /‘Function to
 display details of agent*/
 int RAno () {return ANo; }
 void UpdateMobile() /‘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. RAno ()==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" <
 else
 cout<< "Agent not in the Agency"<
 F. close ();
 }

Answer - 15 : -

Statement 1:
A. Update Mobile ();
Statement 2:
F. seekg (-l*size of (A),ios:: (cur);

Question - 16 : -
Fill in the blanks marked as Statement 1 and Statements 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 Change Mobile () //Function to change Mobile
 {
 cout<<"Changed Mobile:";
 gets (mobile);
 }
 };
 void RepUpdate ()
 {
 fstream F;
 F. open ("REP. DAT", ios : : binaryl 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 1: 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" <
 else
 cout <<"Rep not in the Medical"< F. close ();
 }

Answer - 16 : -

Statement 1:
M. Change Mobile ();
Statement 2:
F. seekp (-1* sizeof (M), ios :: (ur);

Question - 17 : -
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 [2 0] ; //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 GetAcodeO {return Acode;}
 };
 void ChangeCommission (long 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, size of (A)))
 { if (A. GetAcode ()==AC)
 {
 Changed = 'Y';
 A. Update (CM);
 //Statement 1: To place file pointer to the required position
------------------------;
 //Statement 2: To write the object A on to the binary file
 -------------------------;
 }
 }
 if (Changed=='N')
 cout <<"Agent not registered.";
 F. close ();

Answer - 17 : -

Statement 1:
F. seekg (position);
Statement 2:
F. write ((char *) & A, size of (A));

Question - 18 : -
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 a 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(Modify && File,read((char*)&CU, sizeof(CU)))
 {
 if (CU.GetCno()==ChangeCno)
 {
 CU.ModifyEmail ();
 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... "<
 else
 cout <<"Customer not found... "<
 File. close();
 }

Answer - 18 : -

Statement 1:
File.seekp (-l*size of (CU), ios ::cur);
Statement 2:
File.write ((char*)&CU, sizeof (CU));

Question - 19 : -
A binary file “Students.dat” contains data of 10 students where each student’s data is an object of the following class:

class Student
 {
 int Rno; char Name [20] ;
 public:
 void EnterDataO {cin>>Rno; cin. getline (Name, 20)}
 void showData( ) {cout<
 };
With reference to this information, write output of the following program segment:

 ifstream file; Student S;
 File.open ("Students.dat", ios::binary |ios::in);
 File.seekg (0, ios::end);
 Cout<

Answer - 19 : -

A1
B2
C3

Question - 20 : -
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 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==l)
×