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.

Answer)

  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.

Answer)

  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.

Answer)


#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;
}

Output-

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.

Answer)


#include <iostream>
#include <fstream>
using namespace std;
// Class to define the properties
class Employee
{
public:
string Name; i
nt Employee_ID;
int Salary;
};
int main()
{
Employee Emp_1;
Emp_1.Name="Aastha";
Emp_1.Employee_ID=2021;
Emp_1.Salary=20000;
//Writing this data to Employee.txt
ofstream file1;
file1.open("Employee.txt", ios::app);
file1.write((char*)&amp;Emp_1,sizeof(Emp_1));
file1.close();
//Reading data from Employee.txt
ifstream file2;
file2.open("Employee.txt",ios::in);
file2.seekg(0);
file2.read((char*)&amp;Emp_1,sizeof(Emp_1));
printf("\nName :%s",Emp_1.Name);
printf("\nEmployee ID :%d",Emp_1.Employee_ID);
printf("\nSalary %d",Emp_1.Salary);
file2.close();
return 0;
}

Output-

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.

Answer)


#include&lt;fstream&gt;
#include&lt;iostream&gt;
using namespace std;
main()
{
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:
myFile.open("Hello.txt", ios::out);
if(myFile) //Error Checker:
{
myFile<<ch;
cout<<"Data store Successfully: \n\n"<<endl;
}
else cout<< " Error while opening file: "&<<endl;
myFile.close(); //file close:

//opening file for reading:
file.open("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;
}


Output-

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





100 views0 comments

Recent Posts

See All

Comments


Commenting has been turned off.
bottom of page