File handling in C++ Assignment

File handling in C++ assignment contains programming question that most of the time appear in CBSE board Exams.

These assignments are basically divided into two parts . Text file handling and Binary file handling.

Text File Handling – Text file can be read and write in three ways –

  1. Single char  ( read and write )
  2. Single Word ( read and write )
  3. Single line ( read and write )

Binary file operations are basically performed using structure and classes. You are required to read all these questions carefully before attempting any one of them.

Text File handling

Q1.  Write a program in C++ to create a text file in C++.
Q2  Write a program in C++ to create a text file. Read the contents of this text file and display them on the screen.
Q3 Write a program in C++ to read text file and find out total no of characters available in this file.
Q4 Write a program in C++ to read a text file and find out total no of vowels, consonants, digits , punctuation marks available in this text file.
Q5  Write a program in C++ to find out total no of words available in this text file.
Q6  Write a program in C++ to find out total no of lines available in this text file.
Q7  Write a program in C++ to copy the contents of this file in another text file.
Q8  Write a program in C++ to print the contents of this file on printer.
Q9  Write a program in C++ to read a text file and print only those words which starts with vowels.
Q10.  Define a structure of student  having fields admno, name, address. Write a program in C++ to add some records in a file called “student.dat”.
Q11.  Define a structure of student  having fields admno, name, address. Write a program in C++ to read  records from the  file called “student.dat”. and display the same records on the screen
Q12  Define a structure of student  having fields admno, name, address. Write a program in C++ to modify a record  from the  file called “student.dat”.
Q13.  Define a structure of student  having fields admno, name, address. Write a program in C++ to delete  a record  from the  file called “student.dat”.
Q14. Class student

{

private :

int admno;

char   name[30];

char   address[50];

public :

void add_record(void);   // function to add records in the file

void  modify_record(void);  // function to modify record

void  display_record(void);   // function to display record of the file

void  delete_record(void);    // function to delete a record

};

Write the definition of above member functions

Q15. Class employee

{

private:

int empno;

char empname;

public:

void input(void);

void output(void);

};

Write function in c++ to perform

a)      Write a function in C++ to add objects in a binary file

b)      Write a function in c++ to display objects of the binary file

Q15 Assuming the class EMPLOYEE given below, write functions in C++ to perform the  following:-

(i)                 Write  the objects of  EMPLOYEE  to binary file.

(ii)                Reads the objects of EMPLOYEE from binary file and display them on screen.

Class EMPLOYEE

{

int ENC;

char   ENAME[0];

PUBLIC:

Void GETIT(){ cin>> ENO;gets(ENAME);}

Void  SHOWIT() { cout>> ENO<<ENAME;<<endl; }

};

Q16. Consider the following class declaration:

Class  FLIGHT

{

int flight_no;

char  destination[20];

float  distance;

PUBLIC:

void  INPUT(); // read from user

void  Write_File(); // write the data to file

void   OUTPUT(); // read from file & show on screen

};

Compute  the member functions definitions

Q17 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;

};

Wite a function 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.

Q18 Write a C++ program, which reads one line at a time form 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
Q19 Assuming that a text file named TEXT1.TEXT already contains some text written into it, write a       function named vowelwords(), that reads the file TEXT1.TEXT  and creates a new  file named    TEXT2.TEXT,which shall contain only  those words from the file TEXT1.TEXT   which  does not start with an  uppercase vowel(i.e, with ‘A’,’E’,’I’,’O’,’U’). FOR  example, if the file TEXT1.TXT

contains.

Carry umbrella and Overcoat when it  Rains

then the file TEXT2.TXT  shall contain

Carry when it Rains.

Q20 Write a  C++ program, which initializes a string variable to the content. “ Anil  is a great teacher but            unfortunately it kills all its pupils. Harimohan” and output the srtring one character at a time to the   disk file OUT.TXT. You have to include all the header files if required
Q21 Write  a function in C++ to display the data for for a particular book name from  a binary file “BOOK.DAT”, assuming the binary file is contained the objects of the following class:

class BOOK

{

int Bno; char Title[20];

PUBLIC:

int Rbno(){return Bno;}

void Enter(){cin>>Bno;gets(Title);}

void Display(){cout<<Bno<<Title<<endl;}

char * retname() { return Title; }

};

Q22 Write a function in c++ to copy all the lines from file text1.text into text2.txt which is starting from ‘A’ letter. Assuming that each and every line does not exceed from 50 characters.

Leave a Reply