So far, we have learned about the iostream standard library, which provides cin and cout methods for reading from standard input and writing to standard output, respectively. In this tutorial, you will learn how files are handled in C++ programs and what are functions and syntax used to handle them.

Many programming scenarios require handling a large amount of data, and some secondary storage has to be used to store it. The data is stored in the secondary device using the concept of files. Files are a collection of related data stored in a particular storage device. C++ programs can be written to perform read and write operations on these files. Working with files generally requires the following kinds of data communication methodologies:

  • Data transfer between console units.
  • Data transfer between the program and the disk file.

Here are the lists of standard file handling classes:

  1. ofstream: This file handling class in C++ signifies the output file stream and is applied to create files for writing information to files.
  2. ifstream: This file handling class in C++ signifies the input file stream and is applied for reading information from files.
  3. fstream: This file handling class in C++ generally signifies the file stream and can represent both ofstream and ifstream.

All three above classes are derived from fstreambase and the associated iostream class and are explicitly designed to handle disk files.

Opening and Closing a File in C++

If programmers want to use a disk file for storing data, they need to decide the following things about the file and its intended use. These points that are to be noted are:

  • A name for the file.
  • Data type and structure of the file.
  • Purpose (reading, writing data).
  • Opening method.
  • Closing the file (after use).

Files can be opened in two ways. they are:

  1. Using the constructor function of the class
  2. Using member function open of the class

Opening a File in C++

The first operation generally performed on an object of one of these classes to use a file is the procedure known as opening a file. An open file is represented within a program by a stream, and any input or output task performed on this stream will be applied to the physical file associated with it. The syntax of opening a file in C++ is:


open (filename, mode);

There are some mode flags used for file opening. These are:

  • ios::app: append mode.
  • ios::ate: open a file in this mode for output and read/write control to the end of the file.
  • ios::in: open a file in this mode for reading.
  • ios::out: open a file in this mode for writing.
  • ios::trunk: when any file already exists, its contents will be truncated before the file opening.

Closing a File in C++

When any C++ program terminates, it automatically flushes out all the streams, releases all the allocated memory, and closes all the opened files. But it is good to use the close() function to close the file-related streams, which are a member of ifsream, ofstream, and fstream objects.

The structure of using this function is:


void close();

General functions used for File handling

  1. open(): To create a file.
  2. close(): To close an existing file.
  3. get(): to read a single character from the file.
  4. put(): to write a single character in the file.
  5. read(): to read data from a file.
  6. write(): to write data into a file.

Reading from and writing to a File

While coding in C++, programmers write information to a file from the program using the stream insertion operator (<<) and reads the data using the stream extraction operator (>>). The only difference is that for files, programmers need to use an ofstream or fstream object instead of the cout object and ifstream or fstream object instead of the cin object.


#include <iostream>
#include <fstream.h>

void main () {
  ofstream file; ("egone.txt");
  file << "Writing to a file in C++....";

Another program for file handling in C++:


#include <iostream>

void main()
  char c,fn[10];
  cout<<"Enter the file name....:";
  ifstream in(fn);
    cout<<"Error! File Does not Exist";

Another C++ program to print text on the console:


#include <iostream>

void main()
 ofstream fileo("Filethree");
 fileo<<"Hello GS";
 ifstream fin("Filethree");
 char ch;

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram