File Operation Modes in C++

- February 01, 2018
Different operations can be performed on the data file. The basic operations that are usually performed on the data files are given below:
  1. Open: It is used to read data from it to a data file open or to write it into data. The data should be stored in memory before reading or writing data.
  2. Read: It is used to read data from the data file.
  3. Write: It is used to write data or records in the data file.
  4. Close: It is used to close the data file from the memory.
File operations modes in c++

The data files can be opened into different. modes. The opening mode of a file informs the operating system about the type of operation to be performed on the data file.  Different codes are used for opening mode. The file operation modes are defined in ‘ios’ class. The scope resolution operator(: :) is used between the ‘ios’and code for mode. The most important and commonly used modes are as follows:

  • ios::out
  • ios::in
  • ios::app
  • ios::ate
  • ios::trunc
  • ios::noreplace
  • ios::nocreate
  • ios::binary


What is ios::out 

It is used to open data file for output to write data into it. If the data file with the same name already exists on the disk in the same directory, it will be overwritten. Its use is optional for the object of ‘ofstream’ class.


What is ios::in 

It is used to open data file for input to read data from it. In this mode, data can only be read from the file and no data can be written into it. Its use is optional for the object of  'ifstream' class.

What is ios::app

It is used to open data file for output to append data at the end of the existing data file. If file does not exist, then new data file is created with the specified name.

What is ios::ate

It opens file and moves the file pointer to the end of the file for reading or writing data.

What is ios::trunc

In this mode, the existing data file is deleted (or truncated).

What is ios::noreplace

The data file is not replaced if it already exists with the same name in the current directory on the disk. If the file with the same name exists, the open operation fails.

What is ios::nocreate

It is used to open a data file only if it exists in the current directory on the disk. If the specified data file does not exist, then no new file is created and file open operation fails.

What is ios::binary 

It is used to open a data file in binary mode. In this mode, data is stored and read to and from the disk in binary form. It is used with other modes to perform input/output operations on binary files.

     In C++, the opening modes can be combined together with the logical operator ( | ) to perform different combinations of input/output operations on the data file. For example, ifwe want to perform both input and output operations on the data file in binary mode, we can use the following ‘open’ statement:
obj. open(filename, ios::in|ios::out|ios::binary);

In this statement, the “obj” represents the object of elass ‘fstream’.