ofstream ( );
explicit ofstream ( const char * filename, ios_base::openmode mode = ios_base::out );
Construct object and optionally open file
Constructs an object of the ofstream class. This implies the initialization of the associated filebuf object and the call to the constructor of its base class with the filebuf object as parameter.
Additionally, when the second constructor version is used, the stream is associated with a physical file as if a call to the member function open with the same parameters was made.
If the constructor is not successful in opening the file, the object is still created although no file is associated to the stream buffer and the stream's failbit is set (which can be checked with inherited member fail).
Parameters
- filename
- C-string containing the name of the file to be opened.
- mode
- Flags describing the requested i/o mode for the file. This is an object of type ios_base::openmode, which consists on a combination of one or more of the following flags defined as member constants:
flag value | opening mode |
app | (append) Set the stream's position indicator to the end of the stream before each output operation. |
ate | (at end) Set the stream's position indicator to the end of the stream on opening. |
binary | (binary) Consider stream as binary rather than text. |
in | (input) Allow input operations on the stream. |
out | (output) Allow output operations on the stream. |
trunc | (truncate) Any current content is discarded, assuming a length of zero on opening. |
Return Value
none
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// using ofstream constructors.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream outfile ("test.txt");
// >> i/o operations here <<
outfile.close();
return 0;
}
|
This example simply opens and closes a file.
Basic template member declaration
( basic_ofstream<charT,traits> )
1 2
|
basic_ofstream();
explicit basic_ofstream ( const char * filename, ios_base::openmode mode = ios_base::out );
|
See also
|