C++ Files and Streams

By | September 29, 2021

Using the cout<< and cin>> statements of iostream header file we are able to take input and show output data to the user, but in C++ we have a concept known as file handling in which using a C++ program we can create a file that can store data in text format, even if the program is get terminated.

File Handling in C++

In C++ we can use files to store the data permanently, and file handling provides us with a technique to create a file and write and read between it. The input-output operation in C++ use the iostream library, similarly file operation in C++ also use some libraries such as fstream, ifstream, and ofstream, these streams provide an interface between file and program.

File Streams:


It is an input file stream class and it provides input operations for the file. With ifstream we can access get(), getline(), read(), seekg() and tellg() functions to perform the read operations. It is used to read from a existing file.


It is an output file stream and it provides output operations for the file. It provides various methods or functions like put(), write(), tellp() and seekp(). It is used to create a new file or write something new in the file.


It is an input-output stream and it inherits all the properties of ifstream and ofstream class. With fstream, we can perform both read and write operations on a file.


Data Files

As file handling is all about creating file to store data permanently for later use. In file handling we can store data in two types of file format.

  • Text files
  • Binary Files

Text File

In C++ we mostly use the text file to store the information in file handling. In text file, all the data is store in ASCII characters and we can simply open the text file and read data from it because here data store in human-readable form, and each line is terminated with a special character known as End of Line(EOL).


Binary files

The binary file store information in binary format which is not in human-readable form. Here data store in the same format like it is stored in the memory. We do not require any translation in binary files which make them faster as compared to the text files.

File Handling Operations:

Using the file handling we can perform 4 operations on the file:

  • Create a File or Open a File
  • Write to a File
  • Read form file
  • Close the file.

Open A file

Before opening a file we need to mention the stream for the file if we want to read data from an existing file we use the ifstream or if we want to write into a file or want to create a new file we use ofstream or else we can use the fstream for both the operations.

If we want to perform any operation on the file such as read from the file or write in file we need to open the file. If the file is not already in the memory then we can not use the ifstream, then we have to use the ofstream or fstream to create a new file.

Once we have mentioned a stream now we need to open the file for the read and write operations, to open or create a new file we use the open() method.

Syntax to Open a File:

stream_name file_object;
file_object.open(“file_name”, File_mode)


#include <iostream>
#include <fstream>
using namespace std;
int main()
   ofstream file;   // stream of the file is ofstream
   file.open("New_File.txt",ios::out);  // this will create a new txt file by name NEW_FILE

Behind the Code:

This code will create a new txt file in the same folder where the program file is saved. Here we have not performed any operation on the file that’s why the file will be empty. If you run this program twice it will not create a new file it just simply opens the existing one.

Here we have used the ofstream because if the file is not already present in the memory then it will create one by name NEW_File.

ofstream file

Here we have created a stream object of ofstream class, and using ofstream we have set an output stream for the file object.


Here the with the file object we used the open function which accepts two arguments file name and File mode. Here the file name is New_File.txt and file mode is ios::out.

File Mode

The file mode describes how a file to be used, the file mode decides whether we should read from the file or write in the file or append to the file. There are various file modes present for the open() function and the file mode we pass in the open function must have corresponded to it stream type.

File Modes Description Stream type
ios::in Opens the file to read(default for ifstream) ifstream
Ios::out Opens the file to write(default for ofstream) ofstream, ifstream
ios::app Opens the file and appends all the outputs at the end ofstream
ios::ate Opens the file and moves the control to the end of the file ofstream, ifstream
ios::trunc Removes the data in the existing file ofstream
ios::nocreate It throws an error if the file is not already existing in the memory. This mode is used when we do not want to create a new file, and want to perform the read and write operation on the existing file. ofstream
ios::noreplace If the file already exists it then the open function will throw an error. This mode is used when we want to create a new file. ofstream
ios::binary Opens the file in binary mode ofstream, ifstream


By default, all the file mode open the text files, if you want to open the binary file you need to mention the binary mode.

Writing to a file

Once the file is created or opened, we can write in the file, to write in a file we require the stream object of ofstream or fstream.

The stream used to write in a file:
  • fstream
  • ofstream
#include <iostream>
#include <fstream>
using namespace std;
int main()
fstream file;
file.open("NEW_FILE.txt",ios::out);  //mode is ios::out  
file<<"This data has been stored in the file";    //writing in the file
cout<<"The above statement has been written in NEW_FILE.txt";
file.close();   // to close the open file   
return 0;


The above statement has been written in NEW_FILE.txt
In NEW_FILE.txt:
This data has been stored in the file

Behind the Code:

In this above code first, we set the stream object then using the stream object we open the file. Here the file mode is ios::out which means we can only write in the file. Using the file stream object file and insertion pointer << we write the statement in the file.


If you run the above program again and again the statement file<<“This data has been stored in the file”; override the file. If you want to append the file data change the file mode from ios::out to ios::app.

Read From a file

Once we have created a file and write something in it then only, we can perform the read operation on it. To perform a read operation on a file or to read data from a file we need to create an object of file with input stream, and using the fstream and ifstream we can do that.


#include <iostream>
#include <fstream>
using namespace std;
int main()
char line[100];
ifstream file;
file.open("NEW_FILE.txt",ios::in);  //mode is ios::in  
file.getline(line,80);       //reading a line from a file.
cout<<line;                    //showing the line
file.close();                     // to close the open file
return 0;


This data has been stored in the file

Behind the Code:

In this above example we open the file with ios::in mode which mean we can perform the read operation on the NEW_FILE.txt. Once the file is open using the getline(line,80) method or function of ifstream class we read the first 80 characters of the file NEW_FILE.txt, and saved those characters in string line.

Close the file

If we open a file we need to close it too, in the above examples if you do not even close the file it will not affect the program but when we deal with multiple files open statements in one program then we do require the close() function to close the corresponding file.


file.open("NEW_FILE.txt",ios::in);  //mode is ios::in  
file.close();                     // to close the open file

File Handling Quick Summary

  • File handling gives us a technique to store program data in a text or binary file.
  • By default, we use the text file to store the data.
  • Before we create a new file or open the existing one, we require to declare the file stream.
  • There are 3 basic file streams we use in C++ fstream, ofstream, and ifstream.
  • ofstream is used to write in the file, ifstream is used to read from a file and fstream can be used for both.
  • Only with the help of ofstream and fstream we can create a file.
  • To open and create a file we use the open()
  • With the open method, we pass two arguments, file name and file mode.
  • File mode describes the operation that can be performed on the file.

People are also reading: 

Author: Vinay

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published.