Mode

Description

ios::ate Write all output to the end of file (even if file position pointer is moved with seekp)
ios::app

Open a file for output and move to the end of the existing data (normally used to append data to a file, but data can be written anywhere in the file

ios::in

The original file (if it exists) will not be truncated

ios::out

Open a file for output (default for ofstream objects)

ios::trunc

Discard the file's contents if it exists (this is also the default action for ios::out, if ios::ate, ios::app, or ios::in are not specified)

ios::binary

Opens the file in binary mode (the default is text mode)

ios::nocreate

Open fails if the file does not exist

ios::noreplace

Open files if the file already exists.

  • When a file is opened, it must be specified how it is to be opened.
  • This means whether to create it from new or overwrite it and whether it's text or binary, read or write and if the content is to be appended to it.
  • This is done using one or more file mode specifiers

Example:

#include<fstream.h>

#include<conio.h>

Void main()

{

ifstream f1(“student1.txt”);

ofstream f2(“student.txt”);

                char x;

                clrscr();

                while(!f1.eof())

                {

f1.get(x);

f2<<x;

                }

f1.close();

f2.close();

cout<<”File copied successfully..!!”;

getch();

}