basic_ofstream::open

To open or re-open a file stream for output.

void open(const char* s, ios_base::openmode mode = ios_base::out);

Remarks

The function open() opens a file stream for output. The default mode is ios::out, but may be any valid open mode (see below.) If failure occurs open() calls setstate(failbit) which may throw an exception.

There is no return.

Table 1. Legal basic_ofstream file opening modes.
Opening Modes stdio equivalent
Output only  
ios::out "w"
ios::binary | ios::out "wb"
ios::out | ios::trunc "w"
ios::binary | ios::out | ios::trunc "wb"
ios::out | ios::app "a"
Input and Output  
ios::in | ios::out "r+"
ios::binary | ios::in | ios::out "r+b"
ios:: in | ios::out | ios::trunc "w+"
ios::binary | ios::in | ios::out | ios::trunc "w+b"
ios::binary | ios:: out | ios::app "ab"
Listing: Example of basic_ofstream::open() usage:
// Before operation, the file ewl-test contained:
// Chapter One 

#include <iostream>
#include <fstream> 
#include <cstdlib> 

char outFile[] = "ewl-test";
int main()
{
using namespace std;
   ofstream out;
   out.open(outFile, ios::out | ios::app);
      if(!out.is_open()) 
         {cout << "file not opened"; exit(1);}

   out << "\nThis is an annotated reference that " 
      << "contains a description\n"
      << "of the Working ANSI C++ Standard "
      << "Library and other\nfacilities of "
      << "the Embedded Warrior Library.";

   out.close();
   return 0;
}

Result:

After the operation ewl-test contained

Chapter One

This is an annotated reference that contains a description of the Working ANSI C++ Standard Library and other facilities of the Embedded Warrior Library.