Reference <fstream> ofstream open
public member function
<fstream> std::ofstream::openvoid open (const char* filename, ios_base::openmode mode = ios_base::out);
void open (const char* filename, ios_base::openmode mode = ios_base::out);
void open (const string& filename, ios_base::openmode mode = ios_base::out);
Open file
Opens the file identified by argument filename, associating it with the stream object, so that input/output operations are performed on its content. Argument mode specifies the opening mode.
If the stream is already associated with a file (i.e., it is already open), calling this function fails.
The file association of a stream is kept by its internal stream buffer:
Internally, the function calls rdbuf()->open(filename,mode|ios_base::out)
The function sets failbit in case of failure.
The function clears the stream's state flags on success (setting them to goodbit).
In case of failure, failbit is set.
Parameters
filename
String with the name of the file to open.
Specifics about its format and validity depend on the library implementation and running environment.
mode
Flags describing the requested input/output mode for the file.
This is an object of the bitmask member type openmode that consists of a combination of the following member constants:
member constant stands for access
in input File open for reading: the internal stream buffer supports input operations.
out * output File open for writing: the internal stream buffer supports output operations.
binary binary Operations are performed in binary mode rather than text.
ate at end The output position starts at the end of the file.
app append All output operations happen at the end of the file, appending to its existing contents.
trunc truncate Any contents that existed in the file before it is open are discarded.
These flags can be combined with the bitwise OR operator (|).
* out is always set for ofstream objects (even if explicitly not set in argument mode).
Note that even though ofstream is an output stream, its internal filebuf object may be set to also support input operations.
If the mode has both trunc and app set, the opening operation fails. It also fails if both app and in are set simultaneously.
If the mode has both trunc and app set, the opening operation fails.
Return Value
none
If the function fails to open a file, the failbit state flag is set for the stream (which may throw ios_base::failure if that state flag was registered using member exceptions).
Example
1234567891011121314 // ofstream::open / ofstream::close
#include <fstream> // std::ofstream
int main () {
std::ofstream ofs;
ofs.open ("test.txt", std::ofstream::out | std::ofstream::app);
ofs << " more lorem ipsum";
ofs.close();
return 0;
}
Data races
Modifies the ofstream object.
Concurrent access to the same stream object introduce data races.
Exception safety
Basic guarantee: if an exception is thrown, the stream is in a valid state.
It throws an exception of member type failure if the function fails (setting the failbit state flag) and member exceptions was set to throw for that state.
See also
ofstream::is_openCheck if file is open (public member function )ofstream::closeClose file (public member function )filebuf::openOpen file (public member function )