Partager via


basic_ofstream::basic_ofstream

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(
    basic_ofstream&& _Right
);

Parameters

  • _Filename
    The name of the file to open.

  • _Mode
    One of the enumerations in ios_base::openmode.

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

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

Remarks

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 sb.open(_Filename, _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.

Example

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;
        ofs.close();
    }
}

Requirements

Header: <fstream>

Namespace: std

See Also

Reference

basic_ofstream Class

iostream Programming

iostreams Conventions