C++ File Handling

Files and Streams in C++

Whether it is the programming world or not, files are necessary as they store data or information. Files are used to store and secure data in a storage device permanently. File handling provides a tool to store the output of a program in a file and to perform different operations on it.

A stream is an abstraction that outlines a device on which operations of input and output are performed. It refers to the sequence of bytes. A stream can be interpreted as a source or destination of characters of indefinite length depending on its usage.

In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream.

Data TypeDescription
fstreamIt is used to create, read and write information from files.
ifstreamIt is used to read information from files.
ofstreamIt is used to create files and write information to the files.

The above three classes are derived from fstreambase and from the corresponding iostream class and they are designed particularly to maintain disk files.

In the C++ programming language, we have a set of file handling methods. These include ifstream, ofstream, and fstream. These classes are obtained from fstrembase, from the corresponding iostream class. These classes are designed to handle the disk files, are declared in fstream and therefore we must include this file in any program that uses files.

C++ grants us with the following operations in File Handling:

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

Opening a File

Usually, the first operation executed on an object of one of these classes is to correlate it to a real file. This procedure is known as open a file.

We can open a file by utilizing any one of the following methods mentioned below:
1. Bypassing the file name in the constructor at the time of object creation.
2. Or, using the open() function.

A file must be opened before you can read from the file or write to the file. Either ofstream or fstream objects must be used to open a file for writing. And ifstream is used to open a file for reading purposes only.

Syntax for open() function:

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

The first argument specifies the name and location of the file to be opened;
The second argument of the open() function defines the mode of opening the file.

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

Closing a File

When a C++ program language terminates, it automatically cleans all the streams, releases all the allocated memory, and then closes all the opened files.

*But it is always better than a programmer should close the opened files before the termination of the program.

Syntax for close() function:

void close();

Writing to a File

While doing C++ programming, you write data to a file from your program using the stream insertion operator (<<).

Reading from a File

You read data from a file into your program using the stream extraction operator (>>).


#include <iostream>
#include <fstream>
using namespace std;
int main() {
	fstream my_file;
	my_file.open("my_file.txt", ios::out);
	if (!my_file) {
		cout << "File not created!";
	else {
		cout << "File created successfully!";
		my_file << "Guru99";
	return 0;

Code Explanation:

  1. Include the iostream header file.
  2. Include the fstream header file to use its classes.
  3. Include the standard namespace in the program to use its classes without calling it.
  4. Call the main() function.
  5. Create an instance of the fstream class and give it the name my_file.
  6. Use the open() function to create a new file named my_file.txt.
  7. Use an if statement to check whether the file has been opened or not.
  8. Text to print on the console if the file is not opened.
  9. Use an else statement to state what to do if the file was created.
  10. Text to print on the console.
  11. Write some text to the file just created.
  12. Use the close() function.
  13. End of the body of the else statement.
  14. Return a value.
  15. End of the body.
We will be happy to hear your thoughts

Leave a reply

Open chat
Scan the code
Can we help you?