FILE HANDLING
1.a)Aditi has used a text editing software to type some text. After saving the article 3
as WORDS.TXT , she realised that she has wrongly typed alphabet J in place of
alphabet I everywhere in the article. Write a function definition for JTOI() in C++
that would display the corrected version of entire content of the file
WORDS.TXT with all the alphabets “J” to be displayed as an alphabet “I” on
screen .
Note: Assuming that WORD.TXT does not contain any J alphabet otherwise.
Example:
If Aditi has stored the following content in the file WORDS.TXT :
WELL, THJS JS A WORD BY JTSELF. YOU COULD STRETCH THJS TO BE
A SENTENCE
The function JTOI() should display the following content:
WELL, THIS IS A WORD BY ITSELF. YOU COULD STRETCH THIS TO BE
A SENTENCE
b)Write definition of a function CALSAL() in C++ to find the total salary paid to all
the workers in a company. The worker’s detail of this company is stored in a binary
file WORKERS.DAT.Assume that the file WORKERS.DAT is created with the
help of objects of class WORKER, which is defined below : 3
class WORKER
{
int WID; char Name[20];
float Salary;
public:
void INPUT()
{
cin>>WID;gets(Name);cin>>Salary;
}
void OUTPUT()
{
cout<<WID<<":"<<Name<<end1;
cout<<Salary<<endl;
}
float *GetSal(){return Salary;}
};
c)Find the output of the following C++ code considering that the binary file 2
BOOK.DAT exists on the hard disk with a data of 200 books.
class BOOK
{
int BID;char BName[20];
public:
void Enter();void Display();
};
void main()
{
fstream InFile;
InFile.open("BOOK.DAT",ios::binary|ios::in);
BOOK B;
InFile.seekg(5*sizeof(B));
InFile.read((char*)&B, sizeof(B));
cout<<"Book Number:"<<InFile.tellg()/sizeof(B) + 1;
InFile.seekg(0,ios::end);
cout<<" of "<<InFile.tellg()/sizeof(B)<<endl;
InFile.close();
}
2.a.i..Write a function RevText() to read a text file “ Input.txt “ and Print only word
starting with ‘I’ in reverse order . 2
Example: If value in text file is: INDIA IS MY COUNTRY
Output will be: AIDNI SI MY COUNTRY
OR
ii.Write a function in C++ to count the number of lowercase alphabets present in a
text file “BOOK..txt".
b.Write a function in C++ to search and display details, whose destination is Cochin”
from binary file “Bus.Dat”. Assuming the binary file is containing the objects of the
following class: 3
class BUS
{ int Bno; // Bus Number
char From[20]; // Bus Starting Point
char To[20]; // Bus Destination
public:
char * StartFrom ( ); { return From; }
char * EndTo( ); { return To; }
void input() { cin>>Bno>>; gets(From); get(To); }
void show( ) { cout<<Bno<< “:”<<From << “:” <<To<<endl; }
};
OR
c.Write a function in C++ to add more new objects at the bottom of a binary file
"STUDENT.dat", assuming the binary file is containing the objects of the following
class :
class STU
{ int Rno;
char Sname[20];
public:
void Enter( )
{ cin>>Rno;gets(Sname); }
void show( )
{ count << Rno<<sname<<endl; }
};
d.Find the output of the following C++ code considering that the binary file 1
PRODUCT.DAT exists on the hard disk with a list of data of 500 products.
class PRODUCT
{ int PCode;char PName[20];
public:
void Entry();
void Disp();
};
void main()
{ fstream In;
In.open("PRODUCT.DAT",ios::binary|ios::in);
PRODUCT P;
In.seekg(0,ios::end);
cout<<"Total Count: "<<In.tellg()/sizeof(P)<<endl;
In.seekg(70*sizeof(P));
In.read((char*)&P, sizeof(P));
In.read((char*)&P, sizeof(P));
cout<<"At Product:"<<In.tellg()/sizeof(P) + 1;
In.close();
}
OR
d.Which file stream is required for seekg() ?
3.a.A text file named MATTER.TXT contains some text, which needs to be displayed 3
such that every next character is separated by a symbol ‘#’.Write a function definition for
HashDisplay () in C++ that would display the entire content of the file MATTER.TXT
in the desired format.
Example:
If the file MATTER.TXT has the following content stored in it:
THE WORLD IS ROUND
The function HashDisplay () should display the following content:
T#H#E# #W#O#R#L#D# #I#S# #R#O#U#N#D#
b.Write a definition for function TotalTeachers( ) in C++ to read each object of a 2
binary file SCHOOLS.DAT, find the total number of teachers, whose data is stored
in the file and display the same. Assume that the file SCHOOLS.DAT is created
with the help of objects of class SCHOOLS, which is defined below:
class SCHOOLS
{
int SCode; // School Code
char SName[20]; // School Name
int NOT; // Number of Teachers in the school
public:
void Display()
{cout<<SCode<<"#"<<SName<<"#"<<NOT<<endl;}
int RNOT(){return NOT;}
};
c.Find the output of the following C++ code considering that the binary file 1
SCHOOLS.DAT exists on the hard disk with the following records of 10 schools of
the class SCHOOLS as declared in the previous question (4 b).
void main()
{
fstream SFIN;
SFIN.open("SCHOOLS.DAT",ios::binary|ios::in);
SCHOOLS S;
SFIN.seekg(5*sizeof(S));
SFIN.read((char*)&S, sizeof(S));
S.Display();
cout<<"Record :"<<SFIN.tellg()/sizeof(S) + 1<<endl;
SFIN.close();
}
4.(a) Observe the program segment given below carefully, and answer the question that follows :
class Member
{ int Member_no;
char Member_name[20];
public :
void enterdetails( ); //function to enter Member details
void showdetails( ); // function to display Member details
int RMember_no( ); //function to return Member_no
{
return Member_no;
}
};
void Update ( Member NEW)
{
fstream File;
File.open(“MEMBER>DAT”,ios :: binary | ios :: in | ios :: out);
Member OM;
int Recordsread=0, Found=0;
while (!Found && File.read((char*) & OM, sizeof(OM)))
{ Recordsread ++;
if(NEW.RMember_no() == OM.RMember_no( ))
{
……………………… // statement 1
………………………. // statement 2
Found=1;
}
else
File.write((char*) & OM,sizeof(OM));
}
if(!Found)
cout<<Record for modification does not exist”;
File.close( );
}
If the function Update( ) is supposed to modify a record in the file
MEMBER.DAT with the values of Member NEW passed to its argument, write the
appropriate statements for the Statement 1 using seekp( ) or seekg( ) whichever
needed, Statement 2 using read( ) or write( ) method, whichever needed in the above code
nthat would write the modified record at its proper place. [1]
(b) Assume a text file “coordinate.txt” is already created. Using the file create a C++ function to
count the number of words having first character capital. Example :
Do Less Thinking and pay more attention to your heart. Do Less Acquiring and
pay more Attention to what you already have. Do Less Complaining and pay more
Attention to giving.
Output will be : Total words are 11. [2]
(c) Given a binary file “AMOUNT.DAT”, containing records of the given class outstand type.
class outstand
{
int memno;
int outamt;
public :
void getit( )
{ cin>>memno>>outamt; }
void putit( )
{ cout<<memno<<outamt; }
int returnamt( )
{ return outamt; }
};
Write a function in C++ to write objects having outamt more than Rs. 10,000 into CRITICAL.DAT
file. [3]