c++ file handling

File Handling in c++

In this article, you will learn about file handling in the cpp language,

How to create the file in c++,How to open in c++,How to write,and How to close in c++.

Why we need file handling in c++?

Storing the data in the device or anywhere is important. In the same way cpp provide the facility to store the data in the files. After storing data, stored data is used as an instruction. Files are used to store the data on the device permanently.

How to use file handling in c++?

C++ provides the three following classes to perform the input and output operation from the files

=> of stream

=> ifstream

=> fstream

We already used the istream and ostream for cin and cout in the program. In the same way,ifstream and ofstream is used for the input and output from the file. The only difference between iostream and fstream is that fstream is to

deal with physical files.

We already used objects of cin and cout of the iostream class.

Let’s take an example of input and output in files in c++.


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

int main () {
  ofstream ourfile; //create the object

  ourfile.open ("filename.txt"); //call the member function
  ourfile << "This is new file  here you write your code information.\n";

  ourfile.close();                 //close after writing by calling function

  return 0;

In the above example,create the object of the class ourfile , then call the member function to open the file with filename as an argument.
write in the file and at last close it by calling the member function close.

How to open the file in c++?

In order to open the file, the member function of the object of the class stream is used. In the same way, call the member function of the class. Generally, this is the first operation to do on the file and the next step is the opening mode of it.

The syntax of the opening mode:

There are different opening mode.

ios::in = input mode

ios::out    = output mode

ios::binary    = binary mode

ios::ate           = set the initial position at the end of the

ios::app           = append mode

ios::trunc        = delete previous content and replace by new one

By default opening mode:

The default mode is used if we do not give the opening mode in the second argument of the member function.

The object of the class ofstream:ios::out

The object of the class ifstream:ios::in

The object of the class fstream:ios::in or ios::out

How to open the file in multiple modes?

In order to open the file in multiple modes, the bitwise or(  | )operator is used. The bitwise or operator add with a new opening mode like:

Example of Files open in multiple modes

#include <iostream>

#include <fstream>

using namespace std;

int main () {

  fstream ourfile; //create the object


  ourfile.open ("filename.txt", ios::out | ios::in); //open the file but in double mode


  ourfile << "This is new file  here you write your code information.\n";

  ourfile.close(); //close the file after writing



  return 0;


In this example, open the text file in the double mode input and output mode. During the calling of the member function of the object pass another mode with or operator in the second argument. In this way, opening in the two modes.

How to close the files in cpp?

After all the operation or tasks are done on the files then it is compulsory to close.In the end,we call the member function close to flushes the buffers and close the files.

The syntax of the closing file


After calling the close function, we can used the stream objects again for other files, and the closed files also available to open.

Binary files in the cpp:

In the binary files, first of all, create a binary file, second open in binary mode. The input and output operator is independent of the formats. The translation of binary files due to special characters. All the other than the binary is known as text files.

Example of binary files:

#include <iostream>

#include <fstream>

using namespace std;

int main () {

  fstream ourfile; //create the object


  ourfile.open ("filename.bin",ios::in|ios::binary); //open the file but in double mode

  if (ourfile.is_open())


  ourfile << "This is new file  here you write your code information.\n";

  ourfile.close(); //close after writing



cout<<" file is not open"<<endl;


  return 0;


In this example, the binary file is open in the two modes input and binary mode. if the check  is placed to

check whether the file is open or not.

If the file is opened then write the string in it in binary characters and at the end close it.

If the file is not open than the display message it is not opened.

Please write the comment,if you find anything incorrect and any problem in the above topic or you want to share more information about the above topic.


  1. Nice article. valuable for a student. Thank you for sharing this


Post a Comment

Hire a programmer

Popular posts from this blog

CSS selector

C program to convert decimal to binary number

C Program to convert binary to decimal number