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
);

Parameters

  • _Filename
    The name of the file to open.

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

  • _Prot
    The default file opening protection.

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).

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

Concepts

basic_ofstream Class

basic_ofstream Members

iostream Programming

iostreams Conventions