File Handling Assignments

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5
At a glance
Powered by AI
The document discusses various file handling functions in C++ like reading from and writing to text and binary files.

Some of the file handling functions discussed are read(), write(), seekg(), seekp() for positioning in files, open() with different modes like ios::in, ios::out etc.

Some examples of file opening modes discussed are ios::in for input, ios::out for output, ios::ate to set position at end, ios::app for appending.

Assignment – Chapter -7

Data File Handling

2 Marks Questions – Text File Handling


1. Write a user defined function in C++ to read the content from a text file
MYbook.txt, count and display the number of blank spaces present in
it.
2. Write a function in C++ to count the number of alphabets present in a
text file “NOTES.TXT”.
3. Write a function in C++ to count and display the number of words starting
with a vowel present in a given text file “ABC.TXT”.
4. Write a function which reads an already existing file “text1.txt” and write
digits (0-9) in to a new text file “DIGIT.TXT” and non digit files into
NONDIG.TXT.
5. Write a function inC++ to count the number of lines present in a text file
“STORY.TXT”.
6. Assuming that a text file named TEXT1.TXT already contains some text
written into it. Write a function named vowelwords(), that reads the file
TEXT1.TXT and creates a new file named TEXT2.TXT , which shall contain
only those words from the file TEXT1.TXT which don’t start with an
uppercase vowel.
7. Write a program that reads a text file and print it on a printer.
8. Write a program that displays the size of a text file in bytes.
9. Write a function in C++ to print the count of the word is as an
independent word in a text file DIALOGUE.TXT.
For example , if the content of the file is
This is his book. Is this book good?
10. Write an interactive C++ program to read a text file and display the
following :
(i) Frequency table of all the alphabetic characters
(ii) Number of numeric characters present in the file.

3 Marks Questions – Binary Files

1. Assuming the class DRINKS defined below, write functions in C++ to perform
the following:
(i) Write the objects of DRINKS to a binary file.
(ii) Read the objects of DRINKS from binary file and display them on
screen when DNAME has value "INDY COLA".
class DRINKS
{ int DCODE; char DNAME[13]; //Name of the drink
int DSIZE; //Size in liters
float DPRICE;
public:
void getdrinks( )
{cin>>DCODE>>DNAME>>DSIZE>>DPRICE;}
void showdrinks( )

File Handling Assignment Class – XII Page 1


{cout<<DCODE<<DNAME<<DSIZE<<DPRICE<<endl;}
char *getname( ){return DNAME;} };

2. Consider the following class declaration:


class student
{
int addno; char name[90]; float totalmarks;
public :
void getinfo() {cin>>addno>>name>>totalmarks ;}
void showinfo () { cout<< addno<<name<<totalmarks;}
float rettotmarks () { return totalmarks;} };

Give function definition to do the following :


(i) write the objects of student to a binary file.
(ii) Read the objects of student from a binary file and display all the object on
the screen where totalmarks is between 456 and 498 .

3. Write a function in C++ to add new objects at the bottom of a binary file
“STUD.DAT”, assuming the binary file is containing the objects of the
following class.
Class STUD {
int Rno; char Name[20];
public:
void Enter(){cin>>Rno;gets(Name);}
void Display(){cout<<Rno<<Name<<endl;} };

4. Given a binary file APPLY.DAT containing records of the following class


Applicant type
Class Applicant{
char Rno[10]; char name[30]; int A_score;
public:
void enroll()
{ gets(Rno);gets(Name);cin>>A_score; }
void Status()
{ cout<<Rno<<endl<<name<<endl<<A_score; }
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 above 70.

5. Write a function in C++ to search for a BookNo from a binary file


“BOOK.DAT”, assuming the binary file is containing the objects of the
following class.
class Book { int Bno; char Title[30];
public : int RBno()

File Handling Assignment Class – XII Page 2


{ return Bno;}
void Enter() { cin>>Bno;gets(Title);}
void Display(){ cout<<Bno<<Tilte<<endl; } };

6. Given a binary file GAME.DAT, containing records of the following structure


type:
Struct GAME{
Char Gamename[30];
Char participant[10][30];
};
Write a functionin 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”.

7. Following is the structure of each record in a data file named


“PRODUCT.DAT”.
Struct PROD{
char PCODE[10]; char DESC[10]; int stock; };
Write a function in C++ to update the file with a new value of stock. The
value of stock and the PCODE , whose stock to be updated are read during
the execution of the program.

8. Given a binary file TELE.DAT, containing records of the following class


Directory:
class Directory {
char Name[20]; Char Address[30]; char areacode[5]; Char phno[15];

public:
void register(); void show();
int checkcode(char AC[]) { return(strcmp(areacode,AC); } };
Write a function COPYABC() in C++ that would copy only those records
having areacode as “123” from TELE.DAT to TELEBACK.DAT.

One Mark Fill in the Blank Question


1. 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{) ; void showdetails();
int RMember_no() {return Member_no; } };
void Update(Member NEW)
{ fstream File;
File.open(“MEMBER.DAT”,ios::binary|ios::in|ios::out);

File Handling Assignment Class – XII Page 3


Member OM;
int Recordsread = 0, Found = 0;
while (!Found && File.read((char*)&OM, sizeof(OM)))
{ Recordsread ++;
if (NEW.RMember_no() == OM.RMember_no())
{ ___________________//Missing Statement
File.write((char*)&NEW, sizeof(NEW));
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 file


MEMBER.DAT with
the values of Member NEW passed to its argument, write the appropriate
statement for Missing Statement using seekp( ) or seekg( ), whichever
needed, in the above code that would write the modified record at its proper
place.

2. Observe the following program segment given below,and answer the


questions that follows:
class Pracfile { int Pracno; char PracName[20]; int time; int marks;
public:
void Enterp(); //function to enter Pracfile details
void showp() ; //function to display prac details
int RTime(); //function to return time taken
void assign(int M) // function to assign marks
{ marks=M; } };
void allocate()
{ fstream File;
File.open(“MARKS.DAT”,ios::binary|ios::in|ios::out);
Pracfile P;
int rec=0;
while(File.read((char *)&P ,sizeof(P)))
{ if(P.RTime()>50)
P.assign(0);
else
P.assign(10);
____________ // Statement 1
_____________// Statement 2
rec++; } File.close(); }
If the function Allocate() is supposed to allocate marks for the records in the file

File Handling Assignment Class – XII Page 4


MARKS.DAT based on their value of the member time. Write C++ statements for
the statement 1 and statement2 where statement1 is required to position the
file write pointer to an appropriate place in the file and statement 2 is to
perform the write operation with the modified record.
1 Mark Theory Question
1. What are the different functions available for file I/O error handling in c++ ?
2. Why is that all function of ostream class can operate on fstream class?
3. Which class is base class for ostream and istream class?
4. What is difference between (1 mark each)
(i) ios::ate and ios::app (ii) ios::binary and ios::text (iii)seekg() and seekp()
5. What are different file opening modes and their usage?
6. What is difference between Text and Binary File?
7. Which function is used to read and write a block of data?
8. What are the member functions of ofstream class, ifstream class and fstream
class?
9. Write the command to move 20 bytes back from the end of the file.
10. Write the command to move 20 bytes back from the current position?

File Handling Assignment Class – XII Page 5

You might also like