This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft Edge More info about Internet Explorer and Microsoft Edge

Describes an object that controls insertion of elements and encoded objects into a stream buffer of class basic_filebuf< Elem, Tr> , with elements of type Elem , whose character traits are determined by the class Tr . For more information, see basic_filebuf .

Syntax

template <class Elem, class Tr = char_traits<Elem>>
class basic_ofstream : public basic_ostream<Elem, Tr>

Parameters

The basic element of the file buffer.

The traits of the basic element of the file buffer (usually char_traits<Elem>).

Remarks

When the wchar_t specialization of basic_ofstream writes to the file, if the file is opened in text mode it will write an MBCS sequence. The internal representation will use a buffer of wchar_t characters.

The object stores an object of class basic_filebuf< Elem, Tr>.

Example

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

// basic_ofstream_class.cpp
// compile with: /EHsc
#include <fstream>
using namespace std;
int main(int argc, char **argv)
    ofstream ofs("ofstream.txt");
    if (!ofs.bad())
        ofs << "Writing to a basic_ofstream object..." << endl;
        ofs.close();

Constructors

Constructor Description

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

For more information, see basic_ostream, open, and setstate.

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

basic_ofstream::close

Closes a file.

void close();

Remarks

The member function calls rdbuf->close. For more information, see rdbuf and close.

Example

See basic_filebuf::close for an example that uses close.

basic_ofstream::is_open

Indicates whether a file is open.

bool is_open() const;

Return Value

true if the file is open, false otherwise.

Remarks

The member function returns rdbuf->is_open. For more information, see rdbuf and is_open.

Example

// basic_ofstream_is_open.cpp
// compile with: /EHsc
#include <fstream>
#include <iostream>
int main( )
   using namespace std;
   ifstream file;
   // Open and close with a basic_filebuf
   file.rdbuf( )->open( "basic_ofstream_is_open.txt", ios::in );
   file.close( );
   if (file.is_open())
      cout << "it's open" << endl;
      cout << "it's closed" << endl;

basic_ofstream::open

Opens a file.

void open(
    const char* _Filename,
    ios_base::openmode _Mode = ios_base::out,
    int _Prot = (int)ios_base::_Openprot);
void open(
    const char* _Filename,
    ios_base::openmode _Mode);
void open(
    const wchar_t* _Filename,
    ios_base::openmode _Mode = ios_base::out,
    int _Prot = (int)ios_base::_Openprot);
void open(
    const wchar_t* _Filename,
    ios_base::openmode _Mode);

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.

Remarks

The member function calls rdbuf -> open(_ Filename, _Mode | ios_base::out). If that function returns a NULL pointer, the function calls setstate(failbit).

For more information, see rdbuf, open, and setstate.

Example

See basic_filebuf::open for an example that uses open.

basic_ofstream::operator=

Assigns the content of this stream object. This is a move assignment involving an rvalue reference that doesn't leave a copy behind.

basic_ofstream& operator=(basic_ofstream&& right);

Parameters

right
An rvalue reference to a basic_ofstream object.

Return Value

Returns *this.

Remarks

The member operator replaces the contents of the object by using the contents of right, treated as an rvalue reference.

basic_ofstream::rdbuf

Returns the address of the stored stream buffer.

basic_filebuf<Elem, Tr> *rdbuf() const

Return Value

Returns the address of the stored stream buffer.

Example

See basic_filebuf::close for an example that uses rdbuf.

basic_ofstream::swap

Exchanges the contents of two basic_ofstream objects.

void swap(basic_ofstream& right);

Parameters

right
An lvalue reference to another basic_ofstream object.

Remarks

The member function exchanges the contents of this object for the contents of right.

See also

basic_ostream Class
Thread Safety in the C++ Standard Library
iostream Programming
iostreams Conventions