Collapse the table of content
Expand the table of content


Creates an object of type basic_ofstream.

basic_ofstream( );
explicit basic_ofstream(
    const char *_Filename,
    ios_base::openmode _Mode = ios_base::out,
    int _Prot = (int)ios_base::_Openprot
explicit basic_ofstream(
    const wchar_t *_Filename,
    ios_base::openmode _Mode = ios_base::out,
    int _Prot = (int)ios_base::_Openprot
    basic_ofstream&& _Right


The name of the file to open.


One of the enumerations in ios_base::openmode.


The default file opening protection, equivalent to the shflag parameter in _fsopen, _wfsopen.


The rvalue reference to the basic_ofstream object being used to initialize this basic_ofstream object.

The first constructor initializes the base class by calling basic_ostream(sb), where sb is the stored object of class basic_filebuf<Elem, Tr>. It also initializes sb by calling basic_filebuf<Elem, Tr>.

The second and third constructors initializes the base class by calling basic_ostream(sb). It also initializes sb by calling basic_filebuf<Elem, Tr> and then, _Mode | ios_base::out). If the latter function returns a null pointer, the constructor calls setstate(failbit).

The fourth constructor is a copy function. It initializes the object with the contents of right, treated as an rvalue reference.

The following example shows how to create a basic_ofstream object and write text to it.

// basic_ofstream_ctor.cpp
// compile with: /EHsc
#include <fstream>

using namespace std;

int main(int argc, char **argv)
    ofstream ofs("C:\\ofstream.txt");
    if (!ofs.bad())
        ofs << "Writing to a basic_ofstream object..." << endl;

Header: <fstream>

Namespace: std

Community Additions

© 2016 Microsoft