• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • NCERT Solutions
    • NCERT Books Free Download
  • TS Grewal
    • TS Grewal Class 12 Accountancy Solutions
    • TS Grewal Class 11 Accountancy Solutions
  • CBSE Sample Papers
  • NCERT Exemplar Problems
  • English Grammar
    • Wordfeud Cheat
  • MCQ Questions

CBSE Tuts

CBSE Maths notes, CBSE physics notes, CBSE chemistry notes

Chapterwise Question Bank CBSE Class 12 Computer Science (C++) – Data File Handling in C++

Contents

  • 1 Chapterwise Question Bank CBSE Class 12 Computer Science (C++) – Data File Handling in C++
    • 1.1 Exam Practice
      • 1.1.1 Very Short Answer Type Questions [1 Mark]
      • 1.1.2 Short Answer Type Questions [3/4 Marks]

Chapterwise Question Bank CBSE Class 12 Computer Science (C++) – Data File Handling in C++

Exam Practice

Very Short Answer Type Questions [1 Mark]

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

class Agency
{
int ANo;  //Agent Code
char AName[20];   //Agent Name
char Mobi1e[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 UpdateMobile() //Function to update mobile 
{
cout<<"llpdated Mobile:"; 
gets(Mobi1e);
}
};
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<<"Mobi1e Updated for Agent"<<UAno<<endl; 
else
cout<<"Agent not in the Agency"<<endl;
F.close();
}

Answer:

Statement 1

A.UpdateMobile()

Statement 2

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

Question 2:
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 2013

class Customer
{
long int CNo;    //Customer number
char CName[20];    //Customer name
char Emai1C30]; //E-mail of customer public:
void Allocate();
//Function to allocate member 
void Show() ;
//Function to show customer data 
void ModifyEmail()
//Function to modify E-mail
{
cout<<"Enter Modified E-mail:"; 
gets(Email);
}
long int GetCno()
{
return CNo;
}
};
void ChangeData()
{
fstream File;
File.openCCUST.DAT",
ios::binary | ios : :in | ios : : out); 
int Change = 0, Location; 
long int ChangeCno; 
cout<<”Cno - whose E-mail required to be modified:";
cin>>ChangeCno;
Customer CU;
while(!Change&&Fi1e.read((char*)&CU, sizeof(CU)))
{
if(CU.GetCno() == ChangeCno)
{
CU.ModifyEmail();
Location = File.tel 1g()-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<<"Emai 1 Modified..."<<endl; 
else
cout<<"Customer not found... "<<endl;
File.close();
}

Answer:

Statement 1

File.seekp(-1*sizeof(CU),ios::cur) or File.seekp(Location)

Statement 2

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

Question 3:
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 Getlno() (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; 
whi1e(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.close();
}

(i) Write Statement I 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:

(i) Statement 1

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

(ii) Statement 2

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

Question 4:
Observe the program segment given below carefully and fill the blanks marked in 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;
fil.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();
}

Answer:

Statement 1

fil.tellg()

Statement 2

fil.seekp(position-sizeof(product))

or

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

Question 5:
Observe the program segment given below carefully
and fill 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 displaythe 
//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));
____________ //Statement 1
cout<<C.Code<<"==>"<<C.Name<<"==>"
<<C. Popul ati on<<endl ;
File.close();
}
void Country :: Update(int Record)
{
Country C; 
fstream File;
File. open("COUNTRY.DAT",ios::binary
| ios : : in | i os : : out); 
cin>>C.Code;cin.getline(C.Name,20); 
cin>>C. Population;
_________ //Statement 2
File.writeC(char*)&C,sizeof (C));
File.close();
}

Answer:

Statement 1

if(C.Code==Record)

Statement 2

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

Question 6:
Observe the program segment given below carefully and fill the blanks marked as Line 7 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.
void show(); //function to
//display the contents void Purchase(int Tqty)
{
Qty += Tqty;
//function to increment qty
}
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 onto the binary file*/
}
if (Pos=-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(Stock));

Question 7:
Observe the program segment given below carefully and answer the question that follows: All India 2008

class Candidate
{
long Cid;    //Candidate's  Id
char CName[20]; //Candidate's Name 
float Marks; //Candidate's Marks
public:
void Enter(); 
void Display(); 
void MarksChange();
//Function to change marks 
long R_Cid()
{
return Cid;
}
void MarksUpdated(long Id)
{
fstream File;
File, open("CANDIDATE.DAT",
ios::binary | ios::in|ios:rout); 
Candidate C;
int Record = 0,Found = 0; 
while(!Found && File.read
((char*)&C,sizeof(C)))
{
if(Id == C.R_Cid())
{
cout<<"Enter new marks";
C.MarksChange();
____________________ //Statement 1
____________________ //Statement 2     
Found = 1;
}
Record++;
}
if(Found==1)
cout<<"Record updated";
File .close();
}
};

Write the Statement 7 to position the File pointer at the beginning of the record for which the Candidate’s Id matches with the argument passed and Statement 2 to write the updated record at that position.

Answer:

Statement 1

int Pos=File.tellg()-sizeof(C);
File.seekp(Pos);

Statement 2

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

Question 8:
Find the output of the following C++ code
considering that the binary file STUD.DAT exists on the hard disk with records of 90 students.

struct Student
{
char name[31]; 
int roll no;
int marksE5]; 
int total;
};
void main()
{
Student stud; 
fstream file;
int n,size=0; 
file. open("stud.dat",
ios: : in | ios: : binary); 
file.seekg(0,ios;:end); 
size=file.tellg(); 
n=size/sizeof(stud); 
if(n>0)
{
cout<<"\nNumber of records:"; 
cout<<n<<endl ;
}
file.close(); 
getch();
}

Answer:
The output will be
Number of records: 2

Short Answer Type Questions [3/4 Marks]

Question 9:
Write a function CountHisHer() in C++, which reads the contents of a text file diary.txt and count the words His and Her (not case sensitive). Delhi 2013

e.g. if the file contains

Pinaky has gone to his friend's house.
His friend’s name is Ravya. Her house is 12 KM from here.

The function should display the output as:

count for His : 2 
count for Her : 1

Answer:

void CountHisHer()
{
ifstream fil("diary.txt"); 
char word[80]; 
int c1=0,c2=0; 
while(!fi1.eof())
{
fil>>word ;
word[0]=toupper(word[0]);
if(strcmp(word,"His")==0)
c1++;
else if(strcmp(word, "Her")==0) 
c2++;
}
cout<<"count for His"<<cl<<endl ; 
cout<<"count for Her"<<c2<<endl;
fil.close();
}

Question 10:
Assuming the class VINTAGE as declared below, write a function in C++ to read the objects of VINTAGE from binary file VINTAGE.DAT and display those vintage vehicles, which are priced between 200000 and 250000. Delhi 2013

class VINTAGE 
{
int VNO;    //Vehicle Number
char VDesc[10]; //Vehicle Description 
float Price; 
public:
void GET()
{cin>>VNO;gets(VDesc);cin>>Price;}
void VIEW() 
{
cout<<VN0<<endl; 
cout<<VDesc<<endl; 
cout<<Price<<endl;
}
float ReturnPrice()freturn Price;}
};

Answer:

void show()
{
ifstream fcin("VINTAGE.DAT”,
ios::in|ios::binary);
VINTAGE V; 
float Pr;
while(fcin.read((char*)&V,sizeof(V)))
{
Pr = V.ReturnPrice();
if(Pr >= 200000 && Pr <= 250000)
V.VIEW();
}
fcin.close();
}

Question 11:
Write a function EUCountf) in C++, which should reads each character of a text file. IMP.TXT, should count and display the occurrence of alphabets E and U (including small cases e and u too).
e.g. if the file contains is as follows:
Update information is simplified by official websites. The EUCount() function should display the output as: Delhi 2014
E: 4
U: 1

Answer:

void EUCount()
{
ifstream infile("IMP.TXT"); 
char ch;
int countE=0, countU=0; 
while(infile)
{
infile.get(ch); 
if(ch=='E’ || ch=='e') 
countE++;
else if(ch=='U' || ch=='u') 
countU++;
}
cout<<"E : "<<countE<<endl ; 
cout<<"U : "<<countU;
}

Question 12:
Write the function in C++ to read the content of a
text file PLACES.TXT and display all those lines on screen, which are either starting with ‘P’ or starting with ‘S’. Delhi 2012

Answer:

void Display ()
{
ifstream fcin("PLACES.TXT"); 
char Ch[100];
fcin.getline(Ch,100); 
while(fcin)
{
if(Ch[0]=='P'||Ch[0]=='S')
{
Cout<<Ch;
}fcin.getline(Ch,100);
}
fcin.close();
}

Question 13:
Assuming the class GAMES as declared below, write a function in C++ to read the objects of GAMES from binary file GAMES.DAT and display those details of those GAMES, which are meant for children of Age Range “8 to 13”. Delhi 2014

class GAMES
{
int GameCode; 
char GameName[10]; 
char *AgeRange;
public:
void Enter()
{
cin>>GameCode; 
gets(GameName); 
gets(AgeRange);
}
void Display()
{
cout<<GameCode<<":"<<GameName<<endl; 
cout<<AgeRange<<endl ;
}
char *AgeR() {return AgeRange;}
};

Answer:

void READGAMES()
{
GAMES obj;
ifstream infile("GAMES.DAT");
while(infile.read((char*)&obj,sizeof(obj)))
{
if(strcmp(obj.AgeR(),"8 to 13") == 0) 
obj.Display();
}
infile.close();
}

Question 14:
Write a function in C++ to search for the details (Phoneno and Calls) of those phones, which have more than 1000 calls from a binary file phones.dat. Assuming that this binary file contains records/objects of class Phone, which is defined below. All India 2012

class Phone
{
char Phoneno[10]; 
int Calls; 
public:
void get(){gets(Phoneno); 
cin>>Calls;}
void billing() {cout<<Phoneno<<"#"
<<Calls<<end1;}
int getCalls(){return Calls;}
};

Answer:

void show()
{
ifstream fcinCphones.dat",
ios::in|ios::binary);
Phone P;
while(fcin.read((char*)&P, sizeof(P)))
{
if(P.getCalls()>1000)
{
P. billing ();
fcin.close();
}

Question 15:
Write a function in C++ to count the number of “Me” or “My” words present in a text file DIARY.TXT. If the file DIARY.TXT content is as follows:
My first book was Me and My Family.
It gave me chance to be known to the world.
The output of the function should be
Count of Me/My in file : 4 Delhi 2011

Answer:

void count()
{
ifstream finCDIARY.TXT");
char word[10];
int C = 0;
whi1e(!fin.eof())
{
fin>>word;
word[0]=toupper(word[0]);
if((strcmp(word,"Me")==0)||
(strcmp(word,"My")==0))
C++;
}
cout<<"Count of Me/My in file:"<<C; 
fin.close();
}

Question 16:
Write a function in C++ to search()for a laptop from a binary file LAPTOP.DAT containing the objects of class LAPTOP (as defined below). The user should enter the ModeINo and the function should search and display the details of the LAPTOP. Delhi 2011

class LAPTOP 
{
long ModeINo; 
float RAM, HDD; 
char Details[120]; 
public:
void StockEnter()
{
cin>>ModelNo>>RAM; 
cin>>HDD;gets(DetaiIs);
}
void StockDisplay()
{
cout<<ModelNo<<RAM<<HDD<<Details
<<endl;
}
long ReturnModelNo()
{
return ModeINo;
}
};

Answer:

void Search()
{
LAPTOP L; long ModeINo; 
ifstream fin;
cout<<"enter the model no of laptop"; 
cin>>ModelNo;
fin.open("LAPTOP.DAT",ios::binary); 
while(fin.read((char*)&L,sizeof(L)))
{
if(L.ReturnModelNo() == ModelNo)
L.StockDisplay();
}
fin.close();
}

Question 17:
Write a function in C++ to count the words “to” and “the” present in a text file POEM.TXT. (Note that the words “to” and “the” are complete words.) All India 2010

Answer:

void Wordcount()
{
ifstream fin("POEM.TXT");
char word[80];
int WC=0;
while(!fin.eof())
{
fin>>word;
if((stromp(word,"to”)==0)
||(strcmpCword , "the" )=0))
WC++;
}
cout<<WC; 
fin.close();
};

Question 18:
Write a function in C++ to search and display details of all trains, whose destination is “Del hi”from a binary file TRAIN. DAT. Assuming the binary file is containing the objects of the following class: All India 2010

class TRAIN 
{
int Tno;    //Train number
char From[20];    //Train starting point
char To[20];    //Train destination
public:
char *GetFrom()
{ 
return From;
}
char *GetTo()
{
return To;
}
void Input()
{
cin>>Tno;
gets(From);gets(To);
}
void Show()
{
cout<<Tno<<":"<<From<<":"<<To <<endl ;
}
};

Answer:

void Read()
{
TRAIN T; 
ifstream fin;
fin.open("TRAIN.DAT",ios::binary); 
while(fin.read((char*)&T,sizeof(T)))
{
if(strcmp(T. GetTo(), "Del hi" )=0) 
T.Show();
}
fin.close();
}

Question 19:
Write a function COUNT_TO() in C++ to count the presence of a word ‘to’ in a text file NOTES.TXT.
If the content of the file NOTES. TXT is as it is very important to know that smoking is injurious to health, let us take initiative to stop it. The function COUNT_TO() will display the following message Count of -to- in file:3 All India 2009

Answer:

void COUNT_TO()
{
ifstream fin("NOTES.TXT"); 
char str[10]; 
int C=0;
while(!fin.eof())
{
fin>>str;str[0]=tolower(str[0]); 
if(strcmp(str,"to") == 0)
C++;
}
fin.close();
cout<<"Count of-to-in file:"
<<C<<endl;
}

Question 20:
Write a function in C++ to read and display the
details of all the users, whose membership type is V or ‘M’from a binary file CLUB.DAT. Assuming the binary file CLUB.DAT is containing objects of class CLUB, which is defined as follows: All India 2009

class CLUB 
{
int Mno; 
char Mname[20];
//member name 
char Type;
//member type :L Life Member 
// M Monthly Member G Guest 
public:
void Register();
//function to enter the content 
void Display();
//function to display all 
//data members 
char WhatType()
{
return Type;
}
};

Answer:

void DisplayMember()
{
CLUB C; 
ifstream fin;
fin.open("CLUB.DAT",ios::binary);
while(fin.read!(char*)&C.sizeof(C)))
{
if((C.WhatType()=='L')||
(C.WhatType!)=='M'))
C.Display();
}
fin.close();
}

Question 21:
Write a function in C++ to count number of uppercase alphabets present in a text file ARTICLE. TXT.
All India 2008

Answer:

int countupcase()
{
ifstream fin("ARTICLE.TXT");
char ch; 
int count =0;
while(!fin.eof())
{
fin>>ch;
if(isupper(ch)) 
count++;
}
fin.close(); 
return count;
}

Question 22:
Given a binary file PHONE.DAT, containing records of the folio wing structure type: Delhi 2008

class Phonelist 
{
char Name[20];
char Address[30]; 
char AreaCode[5]; 
char PhoneNo[15]; 
public:
void Register(); 
void Show();
int CheckCode(char AC[])
{
return strcmp(AreaCode.AC);
}
};

Write a function TRANSFER() all those records, which are havingAreaCode as “DEL” from PHONE. DAT to PHONEBACK.DAT.

Answer:

void TRANSFER()
{
ifstream fin; 
ofstream fout;
Phonelist ph;
fin.open("PHONE.DAT",
ios:: in | ios::binary); 
fout.open!"PHONEBACK.DAT",
ios: : out | ios :: binary);
while(fin.read((char*)&ph,sizeof(ph)))
{
if(ph.CheckCode("DEL") == 0)
fout.write((char*)&ph,sizeof(ph));
}
fin.close(); 
fout.close();
}

Question 23:
Given a binary file GAME.DAT, containing records of the following structure type:

struct Game 
{
char GameName[20]; 
char Participant[10][30];
};

Write a function in C++ that would read contents from the file GAME.DAT and creates a file named BASKET.DAT copying only those records from GAME.DAT, where the game name is Basket Ball.

Answer:

void CreateNewFi1e()
{
Game g1; 
ifstream fin; 
ofstream fout; 
fin.open("GAME.DAT",
ios::in | ios::binary); 
fout.open("BASKET.DAT", 
ios :: out ] ios :: binary); 
while(fin.read((char*)&gl,sizeof(gl)))
{
if(strcmp(gl.GameName,
"Basket Ball") == 0)
fout.write((char*)&g1,
sizeof(gl));
}
fin.close(); 
fout.close();
}

Question 24:
Following is the structure of each record in a data file named COLONY.DAT:

struct COLONY
{
char Colony_code.[10];
char Colony_name[10]; 
int No_of_People;
};

Write a function in C++ to update the file with a new value of No_of_People. The value of Colony_code
and No_of_People are read during the execution of the program.

Answer:

void update()
{
COLONY c;
int num; 
char col[10]; 
long loc;
cout<<"Enter the colony code:"; 
gets(col );
cout<<"Enter the number of people;"; 
cin>>num;
fstream fileCC0L0NY.DAT".
ios : :in | ios::out|ios:;binary); 
while(!file.eof())
{
file.read((char*)&c,sizeof(c)); 
if(strcmp(c.Colony_code,col)==0)
{
loc=file.tellg()-sizeof(c);
c.No_of_People = num;
file.seekp(loc,ios::beg);
file.write((char*)&c,sizeof(c));
cout<<"file updated";
return;
}
}
cout<<"Colony code not found";
}

Question 25:
Write a function to count the number of words present in a text file named PART.TXT. Assume that each word is separated by a single blank/space character and no blank/space in the beginning and end of the file.

Answer:

void countwords()
{
ifstream fin("PART.TXT",ios::in); 
char wd[80]; 
int count =0; 
while(!fin.eof ())
{
fin>>wd;
if(fin.eof()) break; 
count++;
}
fin.close(); 
cout<<count;
}

Question 26:
Given a binary file APPLY.DAT, containing records of the folio wing class Applicant type:

class Applicant 
{
char A_Rno[10];
//roll number of applicant
char A_Name[30];
//name of applicant 
int A_Score;
//score of applicant
public:
void Enroll()
{
gets(A_Rno); 
gets(A_Name); 
cin>>A_Score;
}
void Status()
{
cout<<setw(12)<<A_Rno; 
cout<<setw(32)<<A_Name; 
cout<<setw(3X<A_Score<<endl ;
}
int returnScore() {return A_Score;}
};

Write a function in C++, that would read contents of file APPLY.DAT and display the details of those students, whose A_Score is below 70.

Answer:

void Display()
{
ifstream fin; 
fin.open("APPLY.DAT",
ios ::in|ios::out|ios::binary); 
Applicant A;
while(fin.read((char*)&A,sizeof(A)))
{
if(A.returnScore()<70)
A.Status();
}
fin .close();
}

Question 27:
Assuming the class Computer as follow:

class Computer 
{
char chiptype[10]; 
int speed; 
public:
void getdetaiIs()
gets(chiptype); 
cin>>speed;
}
void showdetails()
cout<<"Chip"<<chiptype
<<"Speed="<<speed;
}
};

Write a function readfilef) to read all the records present in an already existing binary file SHIP.DAT and display them on the screen, also count the number of records present in the file.

Answer:

void readfile()
{
ifstream fin; 
fin.open("SHIP.DAT",
ios:: in|ios; : binary);
Computer VI; 
int count = 0;
while(fin.read((char*)&VI, sizeof(VI)))
{
count++;
VI.showdetails();
}
fin.close();
cout<<"Total number of records are"
<<count;
}

Question 28:
Assuming that a text file named FIRST.TXT contains some text written into it, write a function named vowelwords, that reads the FIRST.TXT and creates a new file named SECOND.TXT, to contain only those words from the file FIRST.TXT, which start with a lowercase vowel (i.e. with ‘a’, ‘e’, 7′ ‘o’, \u’). e.g. if the FIRST.TXT contains Carry umbrella and overcoat when it rains. Then, the file SECOND.TXT shall contain umbrella and overcoat in it. HOTS

Answer:

#include<fstream.h>
void main()
{
ifstream fin("FIRST.TXT"); 
ofstream fout("SECOND.TXT"); 
char word[25]; 
while(!fin.eof())
{
fin>>word; 
switch(word[0])
{
case 'a': 
case 'e': 
case 'i' : 
case 'o'; 
case 'u' : 
fout<<word<<' ';
}
}
fin.close(); 
fout.close(); 
}

Question 29:
Assuming a binary file FUN. DAT is containing objects belonging to a class LAUGHTER (as defined below). Write a user defined function in C++ to add more objects belonging to class LAUGHTER at the bottom of it.

class LAUGHTER
{
int Idno;    //Identification number
char Type[5];    //LAUGHTER Type
char Desc[255]; //Description 
pub!ic:
void Newentry()
{
cin>>Idno;gets(Type);gets(Desc);
}
void Shownscreen()
{
cout<<Idno<<".”<<Type
<<endl<<Desc<<endl;
}
};

Answer:

void Write0bject()
{
fstream fout("FUN.DAT",
ios::app|ios::binary);
LAUGHTER Lg;
Lg. Newentry();
fout.write((char*)&Lg,sizeof(LAUGHTER)); 
fout.close();
}

Question 30:
Write a user defined function in C++ to read the content from a text file STORY.TXT, count and display the number of alphabets present in it.

Answer:

#include<fstream.h> 
void main()
{
ifstream fin("STORY.TXT"); 
char ch; int count=0; 
if(!fin)
{
cout<<"file does not exists"; 
return;
{
while(1)
}
fin.get(ch);
if(ch == EOF)
break;
if((ch>='A' && ch<='Z' )
||(ch>='a’ && ch<='z'))
count++;
}
cout<<"number of alphabets are:"<<count; 
fin.close();
}

Question 31:
Consider the class declaration

class BUS
{
int bus_no; 
char description[20]; 
int distance; 
public:
void Read();
//to read an object from the
//keyboard
void Write();
//to write an object into a file void Show();
//to display the file 
//contents on the monitor
 };

Complete the member functions definitions.

Answer:

void BUS :: Read()
{
cout<<"\nEnter bus number:"; 
cin>>bus_no;
cout<<"\nEnter description:"; 
gets(description); 
cout<<"\nEnter distance:"; 
cin>>distance;
}
void BUS :: Write()
{
fstream ft;
ft.open("BUS.DAT",
ios::app | ios: :binary); 
ft.write((char*)&this,sizeof(BUS)); 
ft.close();
}
void BUS :: Show()
{
fstream ft; 
ft.open("BUS.DAT",
ios::in|ios::binary); 
while(ft.read((char* )&this,
sizeof(BUS)))
{
cout<<"\nThe bus number is:"
<<bus_no;
cout<<"\nThe description is:"
<<description; 
cout<<"\nThe distance is:"
<<distance;
}
ft. close();
}

Question 32:
Consider the following class declaration HOTS

class employee
{
int code;
char name[20]; 
float salary; 
public:
void input(){cin>>code>>name>>salary;} 
void show() {cout<<code<<name
<<salary<<endl;}
float retsal() {return salary;}
};

Give function definitions to do the following:
(i) Write the objects of employee to a binary file.
(il) Read the objects of employee from a binary file and display all the objects on the screen where salary is between Rs. 7 0000 and Rs. 20000.

Answer:

void data_write()
{ 
employee emp;
//Declares the employee object 
fstream. empfile; 
empfile.open("EMP.dat",
ios: :app|ios::out|ios::binary); 
int n,i; 
clrscr();
cout<<"Enter how many records you want to enter ";
cin>>n;
for(i=0;i<n;i++)
{
emp.input();
empfile.write((char*)&emp,
sizeof(employee));
}
empfile. close();
}
void data_show()
{
employee emp;
// Declares the employee object 
// for read operation 
float tsalary = 0.0;
// A temporary salary 
fstream empfile; 
empfile.open("EMP.dat”,
ios::in|ios::binary); 
empfile.seekg(0,ios;:beg);
if(!empfi1e)
cout<<"File does not exist”; 
while(empfile.read((char*)&emp,
sizeof(employee)))
{
tsalary = emp.retsal();
if(tsalary>=10000&&tsalary<=20000) emp.show();
}
empfi1e.close();
}

Question 33:
Write a C++ program, which reads one line at a time from the disk file TEST.TXT and displays it to a monitor. Your program has to read all the contents of the file. Assume the length of the line not to exceed 80 characters. You have to include all the header files if required.

Answer:

#includeffstream.h>
#include<iostream.h>
#includefstdlib. h>
#include<conio. h>
void main( )
{
ifstream in_file;
char line[80];
in_fi1e.open("TEST.TXT".ios:: in);
if(in_file.eof())
{
cout<<"\n\n File does not exist”;
exit(0);
}
while(! in_file.eof())
{
in_file.getline(1ine,80);
cout<<line<<endl;
}
in_file. close();
getch();
}

Question 34:
A text file named Report.txt exists on a disk. Write a program to create its copy named Finerep.txt, which should be in small letters, but the first letter of the file and first alphabetic character following a full stop should be in uppercase.

Answer:

#include<fstream.h>
#include<conio.h>
#include<process.h>
#include<ctype. h> 
void main()
{
char in_char; 
fstream in_obj,out_obj;
in_obj.open("Report.txt", ios:: in); 
out_obj .open("Finerep.txt",ios: :out); 
if(!in_obj)
{
cerr<<"\n\n*** That file does not exist ***\n";
exit(O);
}
cout<<"\nCopying in_obj.get(in_char); 
in_char = toupper(in_char); 
out_obj.put(in_char); 
while(in_obj.get(in_char))
{
if(in_c h a r == '.')
{
out_obj.put(in_cha r); 
in_obj.get(in_char); 
in_char = toupper(in_char); 
out_obj.put(in_char);
}
else
out_obj,put(in_char);
}
in_obj.close();
out_obj.close();
}

Question 35:
Write a program to read simultaneously the files COUNTRY and CAPITAL and display their contents, i.e. name of country from the file COUNTRY and its capital from the file CAPITAL. HOTS

Answer:

#include<fstream.h>
#include<stdlib.h>
#include<conio. h> 
void main()
{
clrscr(); 
const int m=80; 
char line[m]; 
ifstream fin, fin1; 
fin.open("COUNTRY.TXT"); 
finl.open("CAPITAL.TXT"); 
for(int i=1; i<=10; i++)
{
if(fin.eof()!=0)
{
cout<<"exit"; 
exit(1);
}
fin.getline(line.m); 
cout<<"capital of"<<line; 
if(finl.eof()!=0)
{
cout<<"exit"; 
exit(1);
}
finl.getl ine(line.m); 
cout<<"\n"<<line<<endl;
}
getch();
}

Computer ScienceChapterwise Question Bank for Computer ScienceNCERT Solutions

Primary Sidebar

NCERT Exemplar problems With Solutions CBSE Previous Year Questions with Solutoins CBSE Sample Papers

Recent Posts

  • NCERT Class 10 Science Lab Manual – pH of Samples
  • NCERT Solutions for Class 11 Hindi Core – काव्य भाग – मेरे तो गिरधर गोपाल दूसरो न कोई, पग घुँघरू बाधि मीरां नाची
  • ML Aggarwal Class 7 Solutions for ICSE Maths Chapter 16 Perimeter and Area Ex 16.1
  • NCERT Solutions for Class 12 Chemistry Chapter 12 Aldehydes, Ketones and Carboxylic Acids
  • Magnetic Effects of Electric Current Class 10 Important Questions with Answers Science Chapter 13
  • Question Tags Exercises for Class 8 CBSE With Answers – English Grammar
  • NCERT Solutions for Class 12 Hindi Core – पूरक पाठ्यपुस्तक-सिल्वर वैडिंग
  • Light Reflection and Refraction Class 10 Important Questions with Answers Science Chapter 10
  • ML Aggarwal Class 8 Solutions for ICSE Maths Chapter 18 Mensuration Ex 18.3
  • NCERT Solutions for Class 12 Chemistry Chapter 11 Alcohols, Phenols and Ehers
  • NCERT Solutions for Class 11 Hindi Core – गद्य भाग – जामुन का पेड़
  • MCQ Questions for Class 8 Science Chapter 16 Light with Answers
  • NCERT Class 9 Science Lab Manual – Law of Conservation of Mass
  • NCERT Class 9 Science Lab Manual – Plant and Animal Tissues
  • CBSE Notes for Class 7 Computer in Action – Looping Statements in QBASIC

Footer

Maths NCERT Solutions

NCERT Solutions for Class 12 Maths
NCERT Solutions for Class 11 Maths
NCERT Solutions for Class 10 Maths
NCERT Solutions for Class 9 Maths
NCERT Solutions for Class 8 Maths
NCERT Solutions for Class 7 Maths
NCERT Solutions for Class 6 Maths

SCIENCE NCERT SOLUTIONS

NCERT Solutions for Class 12 Physics
NCERT Solutions for Class 12 Chemistry
NCERT Solutions for Class 11 Physics
NCERT Solutions for Class 11 Chemistry
NCERT Solutions for Class 10 Science
NCERT Solutions for Class 9 Science
NCERT Solutions for Class 7 Science
MCQ Questions NCERT Solutions
CBSE Sample Papers
NCERT Exemplar Solutions LCM and GCF Calculator
TS Grewal Accountancy Class 12 Solutions
TS Grewal Accountancy Class 11 Solutions