Skip to main content

12.4) File Modes


File opening mode can be specified as a second argument when creating an ifstream, ofstream or fstream object.

C++ provides a set of mode constants, including:

  • ios::binary to indicate that a file should be opened in binary mode
  • ios::trunc to indicate that any existing contents of an output file should be discarded (the default when opening files for output)
  • ios::ate to indicate that a stream's position indicator should be put at the end of existing content after opening
  • ios::app to indicate that a stream's position indicator should be put at the end of existing content before each write

The value used for mode is a bitwise OR of the desired constants:

ofstream outfile("data.bin", ios::ate | ios::binary);

See the page on open at cppreference.com for further information.