C++ File Handling

C++ programming language provides the capability to handle files through the code. Other than printing the results on the console programmer can also save results or read data from files. This is called C++ file handling. For this purpose, C++ programming language has fstream library. It is used along with iostream library. The syntax is:

Using the fstream library, programmer can create a new file, open an existing file, write in a file and read from a file. The fstream library defines three data types. The table below shows these data types and their description.

Sr.No.Data TypeDescription
1ofstreamCreates and writes to files
2ifstreamReads from files
3fstreamA combination of ofstream and ifstream: creates, reads, and writes to files
fstream libraries and their description

Using fstream libraries, programmer can create a file, open a file, read from a file and write to a file. Programmer can also close the opened file. All these functions are defined below.

Create a File

In order to create a file, programmer can either use ofstream or fstream library. The syntax to create a file using fstream library is by specifying a file name and file type.

Here, MyFile is a variable of ofstream library. filename.filetype specifies the name and type of file.

Open a File

To open a file open() method is called from ifstream library. It takes two arguments file name and its open mode, the ios. The open() function is:

The filename is mandatory argument while mode is optional. The syntax to open a file using open() function of ifstream is:

It takes two arguments. Here, filename is the name and location of file to be opened and mode is the open mode of file, how programmer wants to open the file. Programmer can also open a file without specifying its open mode. Its syntax is:

Open Modes

The default open modes of fstream libraries are:

Sr.No.LibraryOpen Mode
1ifstreamios::in
2ofstreamios::out
3fstreamios::in, ios::out
Default Open Modes

The table below shows different open modes and their description.

Sr.No.Open ModeDescription
1inStands for input. The internal stream buffer supports input operations. It is used to open the file for reading.
2outStands for output. The internal stream buffer supports output operations. It is used to open the file for writing.
3truncStands for truncate. It is used to erase all the contents of file before opening.
4ateStands for at the end. It is used write at the end of the file.
5appStands for append. It appends the output after the existing contents of the file.
6binaryStands for binary. Instead of texts input output, it performs operations in binary mode.
File Modes and description

Combination of Modes

Programmer can use combination of two or more of mode values. It can achieve by using OR ( | )between two of them. For example, if a programmer wants to open a file in write mode and at the same time wants to truncate it in case that file is not empty, the programmer can use two modes in open() function. Following will be the syntax:

Similarly, if a programmer wants to open a file for both reading a writing, the code for it will be:

Close a File

The best practice is to close a file after it is no longer needed in the code. In this way it frees up the unnecessarily occupied storage before the compiler clears up the allocated memory and closes all opened files. All three libraries fstream, ofstream and ifstream use close() function for this purpose. Its syntax is:

The close() function takes no arguments.

Read from a File

Just like istream and iostream libraries, ifstream and fstream libraries use the stream extraction operator (>>) to read contents from a file. Programmer can then perform various manipulations on the data that is extracted from the file.

Write to a File

Just like ostream and iostream libraries, ofstream and fstream libraries use the stream insertion operator (<<) to write data to a file.

The below code shows how to read and write data from file. After reading the data, the data is saved in variables.

#include <fstream>
#include <iostream>
using namespace std;
int main () {
   char name[100];
   char age[100];
   ofstream myfile;
   myfile.open("testfile.txt");
   cout << "Writing data to the opened file" << endl;
   cout << "What is your name: "; 
   cin.getline(name, 100);
   myfile << name << endl;
   cout << "What is  your age: "; 
   cin >> age;
   cin.ignore();
   myfile << age << endl;
   myfile.close();
   ifstream readfile; 
   readfile.open("testfile.txt"); 
   cout << "Reading data from the file" << endl; 
   cout << "Name present in file: " << endl;
   readfile >> name; 
   cout << name << endl;
   cout << "Age present in file: " << endl;
   readfile >> age; 
   cout << age << endl; 
   readfile.close();
   return 0;
}

The output of the above code after compilation is: