
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 Type | Description |
---|---|
fstream | It is used to create, read and write information from files. |
ifstream | It is used to read information from files. |
ofstream | It 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.No | Mode Flag & Description |
---|---|
1 | ios::app– Append mode. All output to that file to be appended to the end. |
2 | ios::ate– Open a file for output and move the read/write control to the end of the file. |
3 | ios::in– Open a file for reading. |
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. |
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 (>>).
Example
#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";
my_file.close();
}
return 0;
}
Code Explanation:
- Include the iostream header file.
- Include the fstream header file to use its classes.
- Include the standard namespace in the program to use its classes without calling it.
- Call the main() function.
- Create an instance of the fstream class and give it the name my_file.
- Use the open() function to create a new file named my_file.txt.
- Use an if statement to check whether the file has been opened or not.
- Text to print on the console if the file is not opened.
- Use an else statement to state what to do if the file was created.
- Text to print on the console.
- Write some text to the file just created.
- Use the close() function.
- End of the body of the else statement.
- Return a value.
- End of the body.