Importance of File Handling in C++ & How To Do It [2020]


C++ or ‘the New C,’ as it is based on C’s framework and additional features. C++ is also credited to influence several languages such as C# and other newer editions of C. It is also recognized with the introduction of Object-Oriented Programming. This establishes the fact about how essential C++ has been for the programming world. 

This article is about one of the most basic yet crucial tasks, file handing in C++. Now, files are significant for programming as well as for other sectors as they are the storage sectors. This is where the entire data is assembled. The whole concept of file handling can be divided into four sections – 

  • Opening a File
  • Writing to a File
  • Reading from a File
  • Close a file

Importance of File Handling in C++

Before we embark on this journey of C++, let’s take a few moments to understand why do we need file handling. In simple terms, it offers a mechanism through which you can collect the output of a program in a file and then perform multiple operations on it. 

There is one more term, “Stream,” which we’ll be using quite frequently. So, let’s get acquainted with it, as well. A stream is a process that indicates a device on which you are performing the input and output operations. In other words, the stream can be represented as an origin or target of characters of unspecified length based on its function. 

ifstream, ofstream, and fstream make the set of file handling methods in C++. A brief description of these three objects –

  • ofstream – In C++, ofstream is used to create and write in files. It signifies the output file stream. 
  • ifstream – Programmers use ifstream to read from files. It signifies the input file stream. 
  • fstream – fstream can be said as a combination of ofstream and ifstream. It is used to create, read, and write files.

Each one of them helps to manage disk files and, therefore, is specifically designed to manage disk files.

These are the operations used in File Handling in C++  – 

  • Creating a file: open()
  • Reading data: read()
  • Writing new data: write()
  • Closing a file: close()

Must Read: Top 8 Project Ideas in C++

Let’s discuss them thoroughly to understand how file handling in C++ works –

  • Opening a File

Before you can take action on the file, be it read or write, you need to open it first. Ofstream or fstream objects can be applied to initiate a file for writing. Similarly, the ifstream object can be used if you want to read the file. 

You can use the following procedures to open a file – 

  • At the time of object creation, bypass the file name. 
  • Or you can use the open() function. It is a member if ifstream, ofstream, fstream objects.

For example 

void open(const char *nameofthefile, ios::openmode mode);

The first argument in the above defines the name and location of the file which you want to open. The second argument specifies the method by which your target file should be opened. 

Here are the Mode Flag & Description –

  1. ios::app – Append mode. All output to that file to be attached to the end.
  2. ios::in – Open a file for reading. 
  3. ios::ate – Open a file for output and move the read/write control to the end of the file.
  4. ios::out – Open a file for writing. 
  5. ios::trunc – If the file already exists, its contents will be truncated before opening the file.

You can create multiple values using the above modes by using the OR. For instance, if you wish to open a file for reading or writing purpose, use-

fstream newfile; (“file.dat”, ios::out | ios::in );

Similarly, if you wish to open a file in write mode and wish to truncate it if it already exists –

ofstream newfile; (“file.dat”, ios::out | ios::trunc );

  • Writing a file 

While working on a C++ programming file, use either ofstream or fstream object along with the name of the file. It would be best to use the stream insertion operator (<<) to write information to a file. 

#include <iostream>

#include <fstream>

Utilize namespace std; 

int main() {

  // Create and open a text file

  ofstream newFile(“filename.txt”);

  // Write to the file

  NewFile << “Learning files can be challenging, but the result is satisfying enough!”;

  // Close the file



  • Reading a file

For reading a C++ programming file, you use either the fstream or ifstream object. In case you want to read the file line by line, and to print the content of the file, use a while loop along with the getline () function. 

To read information from a file, you need to use the stream extraction operator (>>). 


// Construct a text string, which is managed to output the text file

string newText;

// Read from the text file

ifstream newReadFile(“filename.txt”);

// Now use a while loop together with the getline() function to read the file line by line

while (getline (MyReadFile, myText)) {

  // Output the text from the file

  cout << myText;


// Close the file


  • Closing a file 

By default, when a C++ program closes, it expels all the teams, lets out all the designated memory, and concludes all the opened files. But it is considered to be a healthy practice in terms of file handling in C++ that one should close all the opened files prior to the termination of the program. It also cleans up unnecessary space.

This is the standard syntax for close() function. It is a member of fstream, ifstream, and ofstream objects.

void close();

Also Read: Data Structure Project Ideas


That concluded the lesson on ways in which you can do file handling in C++. Remember, C++ is one of the most predominant languages in the programming world to create both technical and commercial softwares.

Therefore, the more you understand, the more you can explore using this versatile language.If you are interested to learn more and need mentorship from industry experts, check out upGrad & IIIT Banglore’s PG Diploma in Full-Stack Software Development.

Prepare for a Career of the Future

Enroll Now @ upGrad

Leave a comment

Your email address will not be published. Required fields are marked *