Coupon Accepted Successfully!


File Opening Modes

In previous examples, we have learned how to open files using constructors and the open() function using the objects of ifstream and ofstream classes. The opening of the file also involves several modes depending on the operation to be carried out with the file. Theopen() function has the following two arguments:

Syntax of open() function

object.open ( “file_ name”, mode);

Here, the object is a stream object, followed by the open() function. The bracket of the open function contains two parameters. The first parameter is the name of the file, and the second is the mode in which the file is to be opened. In the absence of a mode parameter, a default parameter is considered. The file mode parameters are as shown in Table.

Table: File modes

Mode parameter



Adds data at the end of file


After opening character pointer goes to the end of file

ios:: binary

Binary file


Opens file for reading operation


Opens unsuccessfully if the file does not exist


Opens files if they are already present


Open files for writing operation


Erases the file contents if the file is present

  1. The mode ios::out and ios::trunc are the same. When ios::out is used, if the specified file is present, its contents will be deleted (truncated). The file is treated as a new file.
  2. When the file is opened using ios:app and ios::ate modes, the character pointer is set to the end of the file. The ios:: app lets the user add data at the end of the file, whereas the ios:ate allows the user to add or update data anywhere in the file. If the given file does not exist, a new file is created. The mode ios::app is applicable to the output file only.
  3. The ifstream creates an input stream and an ofstream output stream. Hence, it is not compulsory to give mode parameters.
  4. While creating an object of the fstream class, the programmer should provide the mode parameter. The fstream class does not have the default mode.
  5. The file can be opened with one or more mode parameters. When more than one parameter is necessary, a bit-wise OR operator separates them. The following statement opens a file for appending. It does not create a new file if the specified file is not present.

File opening with multiple attributes

out.open (“file1”, ios::app | ios:: nocreate)


16.9 Write a program to open a file for writing and store float numbers in it.



void main()


float a=784.52, b=99.45,c =12.125;

ofstream out (“float.txt”,ios::trunc);





Explanation: In the above program, the file “float.txt” is opened. If the file already exists, its contents are truncated. The three float numbers are written in the file.

16.10 Write a program to open a file in binary mode. Write and read the data.



int main()



ofstream out;

char data[32];

out.open (“text”,ios::out | ios::binary);

cout<<“\n Enter text”<<endl;


out <<data;


ifstream in;

in.open(“text”, ios::in | ios::binary);

cout<<endl<<“Contents of the file \n”;

char ch;


ch= in.get();


return 0;

Programming In ANSI and TURBO-C
Contents of the file
Programming In ANSI and TURBO-C
Explanation: The above program is similar to the previous one. The only difference is that here files are opened in binary mode.

Test Your Skills Now!
Take a Quiz now
Reviewer Name