File Questions
File Questions
File Questions
(b) 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 GetPrices() { return PRICE; };
};
(c) 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);
count<< ”PRESENT RECORD: "<< POSITION << endl;
MF.close();
}
[2+3+1=6]
Answer
(a)
(b)
void COSTLY()
{
GIFTS g;
fstream fin(GIFTS.DAT);
while(fin.read((char*)&g, sizeof(g))
{
if(g.GetPrice()>2000)
g.View();
}
}
(c)
PRESENT RECORD: 3
2014
(a) 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.Rno()==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 ”<< URno<< endl;
else
cout<< ”Rep not in the Medical”<< endl;
F.close();
}
(b) Write a function EUCount() in C++, which should read 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).
Example-
If the file content is as follows:
Updated information
is simplified by official websites.
The EUCount() function should display the output as
E:4
U:1
(c) 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 AgeRage “8 to 13”.
Class GAMES
{
int GameCode;
char GameName[10];
cahr AgeRange;
public:
void Enter()
{
cin >> GameCode;
gets(GameName);
gets(AgeRange);
}
void Display()
{
cout << GameCode<< ”:”<< GameName<< endl;
cout << AgeRange<< endl;
}
char *AgeR()
{
return AgeRange;
}
};
[1+2+3=6]
Marks:6
Answer:
(a)
Statement 1: F.ChangeMobile();
char ch;
int ecount=0,ucount=0;
while(!fin.eof())
{
fin.get(ch);
if((ch==’E’)||(ch==’e’)
ecount++;
if((ch==’U’)||(ch==’u’)
ucount++;
}
cout << "E :"<< ecount;
cout << "U :"<< ucount;
fin.close();
}
(c)
void DisplayAgeRange()
{
GAMES C;
fstream fin;
fin.open(“GAMES.DAT”, ios:: binary | ios::in);
while(fin.read((char*)&C, sizeof(C)))
{
if(C.AgeR()>==8|| C.AgeR()<=13)
C.Display();
}
fin.close();
}
2013
(a) Fill in the blanks marked as Statement 1 and Statement 2, in the program segment
given below with the 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()
{
cout << ”Enter Changed Email: ”;
cin >> Email;
}
return MNo;
};
void ModifyData()
fstream File;
File.open(“CLUB.DAT”, ios::binary|ios::in|ios::out);
Club CL;
________________________________;
//Statement 2: To write the object SL to the binary file
________________________________;
Modify++;
}
}
if(Modify)
cout << ”Email Changed...”<< endl;
else
cout << ”Member not found…”<< endl;
File.close();
}
(b) Write a function CountYouMe() in C++, which reads the contents of the text file
story.txt and counts the words You and Me (not case sensitive).
class ANTIQUE
{
int ANO;
char Aname[10];
float Price;
public:
void BUY()
{
cin >> ANO;
gets(Aname);
cin >> Price;
}
void show()
{
cout << ANO<< endl;
cout << Aname << endl;
cout << Price<< endl;
}
float GetPrice()
return Price;
};
[1+2+3 = 6]
Marks:6
Answer:
(a).
(i) File.seekg(Position);
(ii) File.write((char*)&CL,sizeof(CL));
(b)
void CountYouMe()
{
char str[40];
fstream fin;
int countYou, countMe;
countYou=countMe=0;
fin.open(“story.txt”, ios::in|ios::out)
while(fin.eof()!=0)
{
fin >> str;
if( (strcmp(str,”You”))
countYou++;
if( (strcmp(str,”Me”))
countMe++;
}
cout << ”n Count for You: ”<< countHis;
cout << ”n Count for Me: ”<< countHer;
}
(c)
void Search()
{
ANTIQUE V;
fstream file;
file.open(“ANTIQUE.DAT”,ios::binary||ios::in);
while(file.read((char*)&V,sizeof(V))
{
if((V.GetPrice()>=10000) && (V.GetPrice()<=15000)
V.SHOW();
}
}
2012
a) Observe the program segment given below carefully and 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 PurchaseItem(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.GetIno())
{
S.Purchase(PQty);
_____________ //Statement1
_____________ //Statement2
Success++;
}
}
if(Success==1)
cout<< ” Purchased Updated”<< endl;
else
cout<< ” Wrong Item 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 item.
(ii) Write statement 2 to perform the write operation so that the updation is done
in the binary file.
b) Write a function in C++ to read the content of a text file ”DELHI.TXT” and display all
those lines on screen, which are either starting with ‘D’ or starting with ‘M’.
class Phone
{
char Phoneno[10];
int Calls;
public:
void Get()
{
Gets(Phoneno);
cin>>Calls;
}
void Billing()
{
cout<< Phoneno<< ”#”<< Calls<< endl;
}
int GetCalls()
{
return Calls;
}
};
[1+2+3 =6]
Marks:6
Answer:
(a). (i) File.seekg(-1*sizeof(S), ios::cur);
(ii) File.write((char*)&S,sizeof(S));
(b).
void readLine()
{
char str[80];
fstream fin;
fin.open(“DELHI.TXT”, ios::in|ios::out)
while(fin.eof()!=0)
{
fin.getline(str,80);
if( (str[0]==’D’ ) || (str[0]==’M’))
cout<< str;
}
}
(c).
void searchPhone()
{
fstream fin;
fin.open(“phones.dat”, ios::in | ios::out)
Phone ph;
while(fin.read((*char)&ph, sizeof(ph))
{
if(ph.GetCalls()>800)
ph.Billing();
}
}
2011
a) Observe the program segment given below carefully and fill
the blanks marked as Statement 1 and Statement 2 using
seekg(), seekp(), tellp() and tellg() functions for performing the
required task.
#include< fstream.h >
class ITEM
{
int Ino; char Iname[20]; float Price;
public :
:
void ModifyPrice(); //The function is 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();
}
b) Write a function in C++ to count the no. of “He” or “She” word present in a text file
“STORY.TXT”.
If the file “STORY.TXT” content is as follows:
He is playing in the ground. She is playing with her dolls.
The output of the function should be
Count of He/She in file : 2
c) Write a function in C++ to search for a camera from a binary file “CAMERA.DAT”
containing the objects of class CAMERA (as defined below). The user should enter the
Model No and the function should search and display the details of the
camera.
class CAMERA
long ModelNo;
float MegaPixel;
int Zoom;
char Details[120];
public :
void Enter()
cin>>ModelNo>>MegaPixel>>Zoom;
gets(Details);
void Display()
cout << ModelNo << MegaPixel << Zoom << Details <<
endl;
long GetModelNo()
{
return ModelNo;
};
[1+2+3 = 6]
Marks:6
Answer:
(a)
int FilePos = File.tellg(-1*sizeof(Item), ios::cur);
File.seekg(FilePos);
(b)
void count_word()
{
fstream tfile(“STORY.TXT”, ios :: in);
char str[20];
int c=0;
clrscr();
while(tfile)
{
tfile>>str;
if((strcmp(str,"He")==0)||(strcmp(str,"She")==0))
c++;
}
cout << " Count of He/She in the file is" << c;
}
(c)
void search()
CAMERA obj;
int MNo;
cin>>MNo;
while(!fi.eof())
fi(obj.GetModelNo()==MNo)
obj.Display();
found = ‘y’;
break;
2010
a) Observe the program segment given below carefully and fill the blanks marked as
statement 1 and statement 2 using tellg() and seekp() functions for performing the required
task. (1)
# include < fstream.h >
class Customer
{
long Cno; char Name [ 20 ] ,Mobile [ 12 ] ;
public:
void Enter ();
void Modify ();
long GetCno() { return Cno; }
};
void ChangeMobile ()
{
Customer C;
fstream F;
F.open (“CONTACT.DAT”, ios :: binary | ios :: in | ios :: out);
long Cnoc; //Customer no. whose mobile number needs to be changed
cin >> Cnoc;
while (F.read((char*) &C, size of (C)))
{
if (Cnoc == C.GetCno())
{
C.Modify();
int Pos=______ //statement 1
//TO find the current position of the file pointer
//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();
}
b) Write the function in C++ to count the words “to” and “the” present in a text file
“POEM.TXT”. (2)
[Note the words “to” and “the” are complete words.]
c) Write a function in C++ to search and display details of all trains, whose destination is
“Delhi” from a binary file “TRAIN.DAT”. Assuming the binary file is containing the
objects of the following class. (3)
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;)
};
Marks:6
Answer:
a) Statement 1:
F.tellg();
Statment2:
F.Seekp(Pos-sizeof(C ));
Or F.seekp(-1* sizeof(C ),ios::cur);
b) void COUNT()
{
ifstream Fil;
Fil.open(“POEM.TXT”);
char Word [ 80 ] , Ch;
int C1=0, C2=0, I=0;
while(!Fil.get(Ch))
{
if(Ch!=’ ’)
Word [ I++ ] = Ch;
else
{
Word [ I ] =’’;
if(strcmp(Word, “to”)==0)
C1++;
else if (strcmp(Word, “the”)==0)
C2++;
I=0;
}
}
cout << ”Count of –to- in file: “ << C1;
cout << ”Count of –the- in file: “ << C2;
}
c)
void Read()
{
TRAIN T;
ifstream fin;
fin.open(“TRAIN.DAT”, ios::binary);
while(fin.read((char*) &T,sizeof(T))
{
if(strcmp(T.GetTo(), ”Delhi” )==0)
F.Show();
}
fin.close(); // Ignore
}
2009
(a) Observe the program segment given below carefully and fill the blanks marked as Line
1 and 2 using fstream functions for performing the required
task.
[1]
# include < fstream.h>
class Library
{
long Ano; //Ano – Accession Number of the Book
char Title [20]; // Title – Title of the Book
int Qty; // Qty – Number of Books in Library
public:
void Enter(int); // Function to enter the content
void Display(); // Function to display the content
void Buy(int Tqty)
{
Qty+=Tqty;
} // Function to increment in Qty
long GetAno () {return Ano;}
};
void BuyBook (long BANo, int BQty)
// BANo® Ano of the book purchases
// BQty ® Number of books purchased
{
fstream File;
File. open (“STOCK.DAT”, ios : : binary ios : : in ios : : out) ;
int Position=-1;
Library L;
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 () ;
}
(b) Write a function COUNT_TO in C++ to count the presence of a word ‘to’ in a text file
“NOTES.TXT”. [2]
Example:
If the content of the file “NOTES.TXT” is as follows:
It is very important to know that
Note: In the above example, ‘to’ occurring as a part of word stop is not considered.
(c) Write a function in C++ to read and display the detail of all the members whose
membership type is ‘L’ or ‘M’ from a binary file “CLUB.DAT”. Assume the binary file
“CLUB.DAT” contains objects of class CLUB, which is defined as
follows: [3]
class CLUB
{
int Mno; //Member Number
char Mname [20]; //Member Name
char Type; //Member Type: L Life Member M Monthly Member
public:
void Register (); //Function to enter the content
void Display (); // Function to display all data members
char WhatType () {return Type;).
};
Marks:6
Answer: (a) // Line 1: To place the file pointer to the required position
File.seekp(Position);
---------------------;
// Line 2: To write the object L on to the binary file
File.write(char *) &L, sizeof(L);
---------------------;
(b) void COUNT_TO()
{
ifstream Fil(“NOTES.TXT” );
char STR[10];
int c=0;
while (Fil.getline(STR, 10, ’ ‘ ))
{
if (strcmp(STR, “to“)==0)
c++;
}
Fil.close();
cout<< "Count of –to-in file “<< c << endl;
}
(c) Void DisplayDemo ()
{
CLUB CBJ;
ifstream fin;
fin.open (“CLUB.DAT”, ios::binary);
while ( fin.read (char*) &CBJ, sizeof(CBJ) )
{
if(CBJ.WhatType()==’L’ || ‘M’ )
CBJ.Display();
}
fin.close();
}
__________________________________________________________________________-----
2008
(a) Observe the program segment given below carefully, and answer the question that follows: [1]
class Applicant
{
long AId;
char Name[20];
float score;
public:
void Enroll();
void Disp();
void MarksScore();
long R_AID() { return AId;)
};
void ScoreUpdate(long Id)
{
fstream File;
File.open("APPLI.DAT", ios::binary|ios::in|ios::out);
Applicant A;
int Record=0, Found=0;
while(!Found && File.read((char*) & C, sizeof(c)))
{
if(Id==A.R_AId())
{
cout<< "Enter new Score";
A.MarksScore ();
-----------------
-----------------
Found=1;
}
Record++;
}
if(Found==1) cout << "Record Updated";
File.close();
}
Write the statement1 to position the File Pointer at the beginning of the record for which the
applicant’s Id matched with the argument passed, and statement2 to write the updated record at that
position.
(b) Write a function in C++ to count the number of lowercase alphabets present in a text file
“BOOK.TXT”. [2]
(c) Given a binary file PHONE.DAT, containing records of the following structure type [3]
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() in C++, that would copy all those records which are having AreaCode
as “DEL” from PHONE.DAT to PHONBACK.DAT
Marks:6
Answer:
(a) The statement to position the file pointer at the beginning of the record for which the
applicant's Id matched with the arguement passed is:
File.seekg(-1*sizeof(A),ios::cur);
(b)
#include< stdlib.h >
#include< iostream.h >
#include< conio.h >
#include< stdio.h >
#include< fstream.h >
void main()
{
int i=0;
char a;
clrscr();
ifstream file1;
file1.open(“BOOK.TXT”, ios::in);
if(!file1)
{
cout << ”File does not exit”;
exit(0);
}
while(file1)
{
file1.get(a);
if(a > = ‘a’ && a < = ‘z’ )
i=i+1;
}
file1.close();
cout << ”The Total number of lowercase letters are:” << i;
}
(c)
void TRANSFER()
{
Phonelist P;
ifstreamfin(" PHONE.DAT" , ios :: in | ios :: binary);
ifstreamfout(" PHONEBACK.DAT", ios :: out | ios :: binary);
while (!fin.eof())
{
fin.read((char *) & P, sizeof(P));
fout.write(char *) & P, sizeof(P));
}
fin.close();
fout.close();
}