Apache C++ Standard Library Reference Guide

## basic_ofstream

Library: Input/output

    basic_ofstream  basic_ostream  basic_ios  ios_base

- Local Index
- Summary
- Synopsis
- Description
- Interface
- Member Types
- Nonmember Types
- Constructors
- Member Functions
- Example
- See Also
- Standards Conformance

### Local Index

#### Members

basic_ofstream() char_type close() | int_type is_open() off_type | open() pos_type rdbuf() | traits_type

#### Non-Members

ofstream | wofstream |

### Summary

Class that supports writing into named files or other devices associated with a file descriptor

### Synopsis

    #include <fstream>

    namespace std {
      template<class charT, class traits = char_traits<charT> >
      class basic_ofstream;
    }

### Description

The class template basic_ofstream supports writing into named files or other devices associated with a file descriptor. It uses a basic_filebuf object to control the associated sequences. It inherits from basic_ostream and can therefore use all the formatted and unformatted output functions.

### Interface

    namespace std {
      template<class charT, class traits = char_traits<charT> >
      class basic_ofstream
            : public basic_ostream<charT, traits>
      {
       public:

        typedef charT                       char_type;
        typedef traits                      traits_type;
        typedef typename traits::int_type   int_type;
        typedef typename traits::pos_type   pos_type;
        typedef typename traits::off_type   off_type;

        basic_ofstream();

        explicit
        basic_ofstream(const char*,
                      ios_base::openmode = ios_base::out);

        // extensions:
        explicit
        basic_ofstream(const char*, ios_base::openmode, long);
        explicit
        basic_ofstream(int, char_type* = 0,
                        streamsize = /* default size */);
        explicit
        basic_ofstream(FILE*, char_type* = 0,
                       streamsize = /* default size */);

        basic_filebuf<charT, traits> *rdbuf() const;

        bool is_open();
        void open(const char *,
                  ios_base::openmode mode = ios_type::out);

        // extensions:
        void open(const char*, ios_base::openmode, long;
        void open (int, char_type* = 0,
                   streamsize = /* default size */);
        void open (FILE*, char_type* = 0,
                   streamsize = /* default size */);

        void close();

      };
    }

### Member Types

    char_type

The type `char_type` is a synonym for the template parameter `charT`.

    off_type

The type `off_type` is a synonym of type `traits::off_type`.

    int_type

The type`int_type` is a synonym of type `traits::int_type`.

    pos_type

The type `pos_type` is a synonym of type `traits::pos_type`.

    traits_type

The type `traits_type` is a synonym for the template parameter `traits`.

### Nonmember Types

    ofstream

The type `ofstream` is a specialization of class basic_ofstream on type `char`:

`typedef basic_ofstream<char> ofstream;`

    wofstream

The type `wofstream` is a specialization of class basic_ofstream on type `wchar_t`:

`typedef basic_ofstream<wchar_t> wofstream;`

### Constructors

    basic_ofstream();

Constructs an object of class basic_ofstream, initializing the base class basic_ostream with the associated file buffer. This buffer is initialized by calling the basic_filebuf constructor:

`basic_filebuf<charT,traits>(rdbuf());`

After construction, a file can be attached to the basic_ofstream object using the `open()` member function.

    explicit
    basic_ofstream(const char* s,
                   ios_base::openmode mode = ios_base::out);

Constructs an object of class basic_ofstream, initializing the base class basic_ostream with the associated file buffer. This buffer is initialized by calling the basic_filebuf constructor:

`basic_filebuf<charT,traits>(rdbuf());`

The constructor then calls:

`rdbuf()->open (s, mode, protection);`

in order to attach the file, whose name is pointed to by `s`, to the basic_ofstream object.

    basic_ofstream(const char* s,
                   ios_base::openmode mode = ios_base::out,
                   long protection = 0666);

Constructs an object of class basic_ofstream, initializing the base class basic_ostream with the associated file buffer. This buffer is initialized by calling the basic_filebuf constructor:

`basic_filebuf<charT,traits>(rdbuf());`

The constructor then calls:

`rdbuf()->open (s, mode, protection);`

in order to attach the file, whose name is pointed to by `s`, to the basic_ofstream object. The third argument, `protection`, holds file permissions. It determines the read/write/execute file permissions under UNIX, but is more limited elsewhere.

> NOTE -- This function is not part of the C++ Standard, but is included here as an extension. See Appendix B for a complete list of extensions of this implementation.

    basic_ofstream(int fd, char_type *buf, streamsize n);

Constructs an object of class basic_ofstream, initializing the base class basic_ostream with the associated file buffer. This buffer is initialized by calling the basic_filebuf constructor:

`basic_filebuf<charT,traits>(rdbuf());`

The constructor then calls:

`rdbuf()->open (fd, buf, n);`

in order to attach the file descriptor, `fd`, to the basic_ofstream object. If the function fails, it sets `ios_base::failbit`.

> NOTE -- This function is not part of the C++ Standard, but is included here as an extension in order to manipulate pipes, sockets, or other UNIX devices that can be accessed through file descriptors. See Appendix B for a complete list of extensions of this implementation.

    basic_ofstream(FILE *fp, char_type* buf, streamsize n);

Constructs an object of class basic_ofstream, initializing the base class basic_ostream with the associated file buffer. This buffer is initialized by calling the basic_filebuf constructor:

`basic_filebuf<charT,traits>(rdbuf());`

The constructor then calls:

`rdbuf()->open (fp, buf, n);`

in order to attach the file pointer, `fp`, to the basic_ofstream object. If the function fails, it sets `ios_base::failbit`.

> NOTE -- This function is not part of the C++ Standard, but is provided here as an extension. See Appendix B for a complete list of extensions of this implementation.

### Member Functions

    void
    close();

Calls `rdbuf ()->close()`; if it fails, calls the basic_ios member function `setstate(failbit)`.

    bool
    is_open();

Returns `rdbuf ()->is_open()`.

    void
    open(const char* s,ios_base::openmode = ios_base::out);

Calls `rdbuf()->open(s, mode)` to open the file; if the call fails, calls the basic_ios member function `setstate(failbit)`.

    void
    open(const char* s, ios_base::openmode, long protection);

Calls `rdbuf()->open(s, mode, protection)` to open the file. If the call fails, calls the basic_ios member function `setstate(failbit)`. The third argument, `protection`, holds file permissions for the read/write/execute file permissions under UNIX. It is more limited under DOS, since files are always readable and do not have special execute permission.

> NOTE -- This function does not appear in the C++ Standard, but is included here as an extension. See Appendix B for a complete list of extensions of this implementation.

    void
    open(int fd, char_type *buf = 0,
         streamsize n = /* default size */);

Calls `rdbuf()->open(fd, buf, n)` to open the file. If this function fails, calls the basic_ios member function `setstate(failbit)`.

> NOTE -- This function is not part of the C++ Standard, but is provided here as an extension. See Appendix B for a complete list of extensions of this implementation.

    void
    open (FILE *fp, char_type *buf = 0,
          streamsize n = /* default size */);

Returns `open(fileno (fp), buf, n)`. `fileno` is a UNIX98 function declared in `<cstdio>`.

> NOTE -- This function is not part of the C++ Standard, but is provided here as a convenience extension. See Appendix B for a complete list of extensions of this implementation.

    basic_filebuf<charT,traits>*
    rdbuf() const;

Returns a pointer to the basic_filebuf associated with the stream.

### Example

See basic_fstream, basic_ifstream, and basic_filebuf examples.

### See Also

char_traits, ios_base, basic_ios, basic_filebuf, basic_ifstream, basic_fstream

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems --Programming Language C++, Section 27.8.1.8