Files and Streams in C++

Files and Streams


Here, in this section we will discuss about files and streams in C++. We know about the iostream standard library which provides cin and out methods for reading from Standard Input and writing the standard output respectively, now will see how we can create files and handle them using C++ programs and the functions that are used handle these files in C ++

files and streams in C++

There are 3 standard file handling streams as follows

  1. Ofstream: It is an output file stream which is used to create files for writing data to the files
  2. Ifstream: it represents input file stream it is used for reading data from the files
  3. fstream: It is viewed as a combination of both ofstream and ifstream i.e. it has  capabilities of both ofstream and ifstream

All the stream classes are specially designed to manipulate disk files

File and Streams in C++

Modes for file opening

File modes will tell what is the purpose of opening a file i.e whether you need to read or write, or what operation you need to perform on that file, etc.

  • ios::app: Append mode
  • ios::ate: Open a file for output and read/write control to the end of the file
  • ios::in: Open file for reading
  • ios::out: Open file for writing
  • ios::trunk: When any file already exists, its existing contents will be deleted before file opening

Opening and closing a file in C plus plus

Before working with file programs in C++  the programmer must have clarity about the following things

  • A name to be given for the file
  • Data type and structure of the file
  • Purpose (reading, writing data)
  • Opening method(file mode)
  • Closing the file (after completing all operations)

Opening a file

  • Before manipulating and performing operations on that file the general operation that is to be performed is opening that particular file
  • A file is opened using a stream and any input and output operation performed in the stream will be also applied to the physical file that it is pointing to

In this program, the file that we are working with must be created in the directory and it is capable of performing both read and write operations


#include <bits/stdc++.h>
using namespace std;
int main()
fstream file; ("example.txt", ios::out | ios::in );
return 0;

Closing a file

 C++ automatically terminates all the file resources and streams associated and  closes all open files when the program terminates but it’s always  a good programming practice to explicitly close the files

#include <bits/stdc++.h>
using namespace std;
int main()
ofstream file; ("prepinsta.txt");
return 0;

Mostly used functions for file handling in C plus plus

  • open(): To create a file
  • close(): To close an existing file
  • get(): To read a single character from the file
  • put(): To write a single character in the file
  • read(): To read data from a file
  • write(): To write data into a file

C++ program to demonstrate for reading from  and writing to a file

We use << and >> to write and read from a file respectively. Let’s see an example.

using namespace std; int main()
char text[200]; fstream file; ("prepinsta.txt", ios::out | ios::in ); cout << "Write text to be written on file." << endl; cin.getline(text, sizeof(text)); // Writing to file file << text << endl; // Reding from file file >> text; cout << text << endl; //closing the file file.close(); return 0; }

The above program asks the user to enter  some text, which is written(added)  into the specified file physically  and all the file contents present in the file are also displayed on the terminal