std::basic_ofstream::basic_ofstream

From cppreference.com
< cpp‎ | io‎ | basic ofstream

basic_ofstream();
(1)
explicit basic_ofstream( const char* filename,
                ios_base::openmode mode = ios_base::out );
(2)
explicit basic_ofstream( const string& filename,                                  
                ios_base::openmode mode = ios_base::out );
(3) (since C++11)
basic_ofstream( basic_ofstream&& other );
(4) (since C++11)
basic_ofstream( const basic_ofstream& rhs) = delete;
(5) (since C++11)

Constructs new file stream.

1) Default constructor: constructs a stream that is not associated with a file: default-constructs the std::basic_filebuf and constructs the base with the pointer to this default-constructed std::basic_filebuf member.
2) First, performs the same steps as the default constructor, then associates the stream with a file by calling rdbuf()->open(filename, mode | std::ios_base::out) (see std::basic_filebuf::open for the details on the effects of that call). If the open() call returns a null pointer, sets setstate(failbit).
3) Same as basic_ofstream(filename.c_str(), mode).
4) Move constructor. First, move-constructs the base class from other (which does not affect the rdbuf() pointer), then move-constructs the std::basic_filebuf member, then calls this->set_rdbuf() to install the new basic_filebuf as the rdbuf() pointer in the base class.
5) The copy-constructor is deleted: this class is not copyable.

[edit] Parameters

filename - the name of the file to be opened
mode - specifies stream open mode. It is bitmask type, the following constants are defined:
Constant Explanation
app seek to the end of stream before each write
binary open in binary mode
in open for reading
out open for writing
trunc discard the contents of the stream when opening
ate seek to the end of stream immediately after open
other - another file stream to use as source

[edit] Example

#include <fstream>
#include <utility>
#include <string>
int main()
{
    std::ofstream f0;
    std::ofstream f1("test.bin", std::ios::binary);
    std::string name = "example.txt";
    std::ofstream f2(name);
    std::ofstream f3(std::move(f1));
}


[edit] See also

opens a file and associates it with the stream
(public member function)
opens a file and configures it as the associated character sequence
(public member function of std::basic_filebuf)
replaces the rdbuf without clearing its error state
(protected member function)
constructs the object
(public member function of std::basic_ostream)