cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : IOstream Library : ofstream : ofstream
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
IOstream Library
manipulators
classes:
· filebuf
· fstream
· ifstream
· ios
· iostream
· ios_base
· istream
· istringstream
· ofstream
· ostream
· ostringstream
· streambuf
· stringbuf
· stringstream
objects:
· cerr
· cin
· clog
· cout
types:
· fpos
· streamoff
· streampos
· streamsize
ofstream
· ofstream::ofstream
member functions:
· ofstream::close
· ofstream::is_open
· ofstream::open
· ofstream::rdbuf

-

ofstream::ofstream constructor member
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 valueopening 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

// 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> )
basic_ofstream();
explicit basic_ofstream  ( const char * filename, ios_base::openmode mode = ios_base::out );

See also

ofstream::open Open file (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us