top of page

File Handling and templates: C plus plus Class Notes

Updated: Oct 22, 2022

Mobiprep has created last-minute notes for all C++ and templates to help you with the revision of concepts for your university examinations. So let’s get started with the lecture notes on C++.

Our team has curated a list of the most important questions asked in universities such as DU, DTU, VIT, SRM, IP, Pune University, Manipal University, and many more. The questions are created from the previous year's question papers of colleges and universities.

File Handling and templates

Question 1) What are the file streams?

Answer) File streams allow input and output to files. In other words, file streams provide an advantage in that you can open a file on construction of a stream, and the file is closed automatically on destruction of the stream.


Question 2) Explain the process of open, read, write and close files.


  1. Opening a file- To open an existing file, command ‘fopen’ is used.

  2. Reading a file- To read an existing file, command ‘fscan or fgets’ is used.

  3. Writing a file- To write to a file, command ‘fprintf and fputs’ is used.

  4. Closing a file- To close a file, command ‘fclose’ is used.


Question 3) Explain the role of seekg(), seekp() ,tellg() ,tellp() function in the process of random access in a file.


  1. seekg() is used to move the get pointer to a desired location with respect to a reference point.

  2. seekp() is used to move the put pointer to a desired location with respect to a reference point.

  3. tellg() is used to know where the get pointer is in a file.

  4. tellp() is used to know where the put pointer is in a file.


Question 4) Explain the Standard Template Library and how it is working.

Answer) programming data structures and functions such as lists, stacks, arrays, etc.

It has four components-

a. Algorithm(Sorting, searching, etc.)

b. Containers:

  • Sequence Containers(vector list, etc.)

  • Container Adaptors(queue, stack, etc.)

  • Associative Containers(multiset, map, etc.)

  • Unordered Associative Containers(unordered_set, etc.)

c. Iterators

d. Functions


Question 5) Write a C++ program using function template to find the product of two integer or floating point type of data.


#include <iostream>
using namespace std;
template <typename T>
T product (T x, T y)
return x * y;
int main()
{ cout<< "Product: " << product (3, 5) << endl;
cout << "Product: " <<product (3.0, 5.2) <<endl;
cout << "Product: " << product (3.24234, 5.24144) <<endl;
return 0;


Product: 15 Product: 15.6
Product: 16.9945


Question 6) Explain File operations.

Answer) A file is an abstract data type. For defining a file properly, we need to consider the operations that can be performed on files like: create, write, read, reposition, delete files.


Question 7) Write a C++ program involving reading and writing of classobjects in a file.


#include <iostream>
#include <fstream>
using namespace std;
// Class to define the properties
class Employee
string Name; i
nt Employee_ID;
int Salary;
int main()
Employee Emp_1;
//Writing this data to Employee.txt
ofstream file1;"Employee.txt", ios::app);
//Reading data from Employee.txt
ifstream file2;"Employee.txt",ios::in);
printf("\nName :%s",Emp_1.Name);
printf("\nEmployee ID :%d",Emp_1.Employee_ID);
printf("\nSalary %d",Emp_1.Salary);
return 0;


Name: Aastha
Employee ID: 2021
Salary: 20000


Question 8) Write a C++ program to update the contents of a file by accessing the contents randomly.


using namespace std;
ofstream myFile; //Object for Writing:
ifstream file; //Object for reading:
char ch[30]; //Arguments: name & size of array.
char text; //character variable to read data from file:

cout<< "Enter some Text here: "<<endl;
cin.getline(ch,30); //Getline function.

//opening file for Writing:"Hello.txt", ios::out);
if(myFile) //Error Checker:
cout<<"Data store Successfully: \n\n"<<endl;
else cout<< " Error while opening file: "&<<endl;
myFile.close(); //file close:

//opening file for reading:"InfoBrother.txt", ios::in);
if(file) //Error Checker:
file.seekg(1, ios::beg); //skip first 1 bytes from beginning:
cout<<" The output (after skipping first 1 byte) is: ";
while(!file.eof()) //read data from file till end of file:
file.get(text); //read data:
cout<<text; //display data:
else cout<<"Error while Opening File: ">>endl;
file.close(); //file close:

return 0;


Enter some Test here:
Hello Data Store Successfully:
The output (after skipping 1 byte) is: Hello

90 views0 comments

Recent Posts

See All


Комментарии отключены.
bottom of page