Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

basic_ofstream Class

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.

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


The basic element of the file buffer.


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

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

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

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;


Creates an object of type basic_ofstream.


Closes a file.


Determines if a file is open.


Opens a file.


Returns the address of the stored stream buffer.


Exchange the contents of this basic_ofstream for the contents of the provided basic_ofstream.


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

Header: <fstream>

Namespace: std

© 2015 Microsoft