Latest Code Tutorials

C++ File Handling: The Complete Guide

Most computer programs work with files because it helps stores the information permanently. The information and data stored under a special name on a storage device are called a file. Stream refers to a sequence of bytes. A file itself is the bunch of bytes stored on storage devices like tape, magnetic disk, etc. The binary file is a file that contains information in the same format as it is stored in memory. In binary files are used for a line, and no translations occur here.

C++ File Handling

To handle files in C++, there are mainly dealt with using the three classes fstream, ifstream, and ofstream available in the fstream header file.

  1. ofstream: Stream class to write on the files
  2. ifstream: Stream class to read from the files
  3. fstream: This Stream class to read and write from the /to files.

Operation performed on the file

  1. Creation of the new file
  2. Opening an existing file
  3. Reading from the file
  4. Writing to the file
  5. Deleting the file

Classes for file stream operation


Stream class to write on files.

Example of using ofstream in program code.

#include <iostream>
#include <fstream>
using namespace std;

int main()
	char data[100];
	ifstream ifile;

	//create a text file before executing."text.txt");
	while (!ifile.eof())
		ifile.getline(data, 100);
		cout << data << endl;
	return 0;

See the following output.


C++ File Handling Example


Stream class to read from files.

Example of using ifstream in program code:-

#include <iostream>
#include <fstream>
using namespace std;

int main()
	char data[100];
	ifstream ifile;

	//create a text file before executing."text.txt");
	while (!ifile.eof())
		ifile.getline(data, 100);
		cout << data << endl;
	return 0;

See the following output.




It can both read and write from/to files.


Sets the file buffers to read and write. It contains close() and open() member function in it.

Opening and closing a file

Let’s Open the File by using the constructor.

#Open File by using constructor

See the following code example.

ifstream (constr char* filename, ios_base::openmode mode = ios_base::in);
ifstream fin(filename, openmode) by default openmode = ios::in
ifstream fin(“filename”);

#Open File by using the open() method

See the following code example.

Calling of default constructor
ifstream filin;               //create an input stream"filename", openmode);         //associate filin stream with file
filin.close();                              //terminate association with file master

Different types of modes


It stands for input and uses to open a file for reading. Support input operations.


It stands for output and uses to open a file from a writing point of view. Supports output operations*


It stands for binary, and operations are performed in binary mode rather than in text format and anything else.


It stands for at the end, and in this output, operations start at the end of the file.


It stands for append, and in this, all the output operations happen at the end of the file, appending to the existing files.


It stands for truncate, and with this, any content before the opening of the file is discarded.

All these flags can be combined with the help of the bitwise operator OR (|). 

For instance, if we want to open a file example.scs in the binary mode to add data, we could do it by the following call to the member function open(): 

fstream file; ("example.scs", ios::out | ios::app | ios::binary);  

Default opening modes






ios::in | ios::out

Closing File

See the following code.



#put() and get() function

The function put() is to write a single character to the associated stream.

The function get() reads a single character from the associated stream.



#write() and read() function

The write() and read() functions write and read blocks of binary data(file).

See the following code. *)&a, sizeof(a));
file.write((char *)&a, sizeof(a));


  1. First, pin down the type of link required.
  2. Declare the stream for the desired type of link.
  3. Attach the desired file to the stream.
  4. Now process as required.
  5. Close the file link with the stream.

File Pointers

All the I/O streams objects have at least one internal stream pointer.

Ifstream is just like istream, which has a pointer called the get pointer to read in the next input by pointing to that element.

Ofstream is just like ostream, and it has a pointer called the put pointer, located at the points where the next element has to be written.

At last, stream, which inherits both the get and put pointers from iostream.

The other prototype for these functions is the following.

seekg(offset, refposition );
seekp(offset, refposition );

The refposition takes one of the following three constants defined in the ios class.




  Start of the file

  The current position of the pointer

  End of the file


file.seekg(-5, ios::beg)

Now, see the following table.

No. Function Description
1 fopen() It opens a new or existing file
2 fprintf() It helps to write data into the file
3 fscanf() It reads data from the file
4 fputc() It writes a character into the file
5 fgetc() It reads a character from a file
6 fclose() It closes the file
7 fseek() It sets the file pointer to the given position
8 fputw() It writes an integer to file
9 fgetw() It reads an integer from a file
10 ftell() It returns the current position
11 rewind() It sets the file pointer to the beginning of the file.


That’s it for file handling in C++.

Related posts

C++ List

C++ Map

C++ unordered_map

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.