ofstream::ofstream [constructor]
ofstream ( );
explicit ofstream ( const char * filename, openmode mode = out | trunc );

Construct an object and optionally open a file.
  Constructs an object of class ofstream. This implies the initialization of the associated filebuf object and the call to its base class' constructor with the filebuf object as constructor parameter.
  Additionally, in case the second constructor syntax is used, the stream's file is associated with a physical file as if a call to member open with the same parameters was made.


String containing the name of the file to be opened.
Flags describing the requested i/o mode for the file. This is an object of type ios_base::openmode, it generally consist of a combination of the following flags (member constants):
app(append) Seek to the end of the stream before each output operation.
ate(at end) Seek to the end of the stream when opening.
binaryConsider stream as binary rather than text.
inAllow input operations on a stream.
outAllow output operations on a stream.
trunc(truncate) Truncate file to zero when opening.

Return Value.


// using ofstream constructors.
#include <iostream>
#include <fstream>
using namespace std;

int main () {

  ofstream outfile ("test.txt");

  // >> i/o operations here <<


  return 0;
This example simply opens and closes an output file.

Basic template member declaration ( basic_ofstream<charT,traits> ):
explicit basic_ofstream  ( const char * filename, openmode mode = out | trunc );

See also.
  ofstream class

© The C++ Resources Network, 2001