public member function
<fstream>

std::basic_ofstream::open

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

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 constantstands foraccess
ininputFile open for reading: the internal stream buffer supports input operations.
out *outputFile open for writing: the internal stream buffer supports output operations.
binarybinaryOperations are performed in binary mode rather than text.
ateat endThe output position starts at the end of the file.
appappendAll output operations happen at the end of the file, appending to its existing contents.
trunctruncateAny 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 basic_ofstream objects (even if explicitly not set in argument mode).
Note that even though basic_ofstream is an output stream, its internal basic_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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 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 basic_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