libstdc++
std::basic_ofstream< _CharT, _Traits > Class Template Reference
Inheritance diagram for std::basic_ofstream< _CharT, _Traits >:
[legend]

Public Types

typedef ctype< _CharT > __ctype_type
 
typedef basic_filebuf< char_type, traits_type > __filebuf_type
 
typedef basic_ios< _CharT, _Traits > __ios_type
 
typedef num_put< _CharT, ostreambuf_iterator< _CharT, _Traits > > __num_put_type
 
typedef basic_ostream< char_type, traits_type > __ostream_type
 
typedef basic_streambuf< _CharT, _Traits > __streambuf_type
 
typedef _CharT char_type
 
typedef traits_type::int_type int_type
 
typedef traits_type::off_type off_type
 
typedef traits_type::pos_type pos_type
 
typedef _Traits traits_type
 

Public Member Functions

 basic_ofstream ()
 
 basic_ofstream (basic_ofstream &&__rhs)
 
template<typename _Path , typename _Require = _If_fs_path<_Path>>
 basic_ofstream (const _Path &__s, ios_base::openmode __mode=ios_base::out)
 
 basic_ofstream (const basic_ofstream &)=delete
 
 basic_ofstream (const char *__s, ios_base::openmode __mode=ios_base::out)
 
 basic_ofstream (const std::string &__s, ios_base::openmode __mode=ios_base::out)
 
 ~basic_ofstream ()
 
template<typename _ValueT >
basic_ostream< _CharT, _Traits > & _M_insert (_ValueT __v)
 
void close ()
 
__ostream_typeflush ()
 
bool is_open ()
 
bool is_open () const
 
template<typename _Path >
_If_fs_path< _Path, void > open (const _Path &__s, ios_base::openmode __mode=ios_base::out)
 
void open (const char *__s, ios_base::openmode __mode=ios_base::out)
 
void open (const std::string &__s, ios_base::openmode __mode=ios_base::out)
 
__ostream_typeoperator<< (__streambuf_type *__sb)
 
__ostream_typeoperator<< (const void *__p)
 
__ostream_typeoperator<< (nullptr_t)
 
basic_ofstreamoperator= (basic_ofstream &&__rhs)
 
basic_ofstreamoperator= (const basic_ofstream &)=delete
 
__filebuf_typerdbuf () const
 
__ostream_typeseekp (off_type, ios_base::seekdir)
 
__ostream_typeseekp (pos_type)
 
void swap (basic_ofstream &__rhs)
 
pos_type tellp ()
 
__ostream_typeoperator<< (__ostream_type &(*__pf)(__ostream_type &))
 
__ostream_typeoperator<< (__ios_type &(*__pf)(__ios_type &))
 
__ostream_typeoperator<< (ios_base &(*__pf)(ios_base &))
 
Inserters

All the operator<< functions (aka formatted output functions) have some common behavior. Each starts by constructing a temporary object of type std::basic_ostream::sentry. This can have several effects, concluding with the setting of a status flag; see the sentry documentation for more.

If the sentry status is good, the function tries to generate whatever data is appropriate for the type of the argument.

If an exception is thrown during insertion, ios_base::badbit will be turned on in the stream's error state without causing an ios_base::failure to be thrown. The original exception will then be rethrown.

__ostream_typeoperator<< (long __n)
 
__ostream_typeoperator<< (unsigned long __n)
 
__ostream_typeoperator<< (bool __n)
 
__ostream_typeoperator<< (short __n)
 
__ostream_typeoperator<< (unsigned short __n)
 
__ostream_typeoperator<< (int __n)
 
__ostream_typeoperator<< (unsigned int __n)
 
__ostream_typeoperator<< (long long __n)
 
__ostream_typeoperator<< (unsigned long long __n)
 
__ostream_typeoperator<< (double __f)
 
__ostream_typeoperator<< (float __f)
 
__ostream_typeoperator<< (long double __f)
 
Unformatted Output Functions

All the unformatted output functions have some common behavior. Each starts by constructing a temporary object of type std::basic_ostream::sentry. This has several effects, concluding with the setting of a status flag; see the sentry documentation for more.

If the sentry status is good, the function tries to generate whatever data is appropriate for the type of the argument.

If an exception is thrown during insertion, ios_base::badbit will be turned on in the stream's error state. If badbit is on in the stream's exceptions mask, the exception will be rethrown without completing its actions.

__ostream_typeput (char_type __c)
 
__ostream_typewrite (const char_type *__s, streamsize __n)
 
__ostream_typeoperator<< (__ostream_type &(*__pf)(__ostream_type &))
 
__ostream_typeoperator<< (__ios_type &(*__pf)(__ios_type &))
 
__ostream_typeoperator<< (ios_base &(*__pf)(ios_base &))
 
Inserters

All the operator<< functions (aka formatted output functions) have some common behavior. Each starts by constructing a temporary object of type std::basic_ostream::sentry. This can have several effects, concluding with the setting of a status flag; see the sentry documentation for more.

If the sentry status is good, the function tries to generate whatever data is appropriate for the type of the argument.

If an exception is thrown during insertion, ios_base::badbit will be turned on in the stream's error state without causing an ios_base::failure to be thrown. The original exception will then be rethrown.

__ostream_typeoperator<< (long __n)
 
__ostream_typeoperator<< (unsigned long __n)
 
__ostream_typeoperator<< (bool __n)
 
__ostream_typeoperator<< (short __n)
 
__ostream_typeoperator<< (unsigned short __n)
 
__ostream_typeoperator<< (int __n)
 
__ostream_typeoperator<< (unsigned int __n)
 
__ostream_typeoperator<< (long long __n)
 
__ostream_typeoperator<< (unsigned long long __n)
 
__ostream_typeoperator<< (double __f)
 
__ostream_typeoperator<< (float __f)
 
__ostream_typeoperator<< (long double __f)
 
Unformatted Output Functions

All the unformatted output functions have some common behavior. Each starts by constructing a temporary object of type std::basic_ostream::sentry. This has several effects, concluding with the setting of a status flag; see the sentry documentation for more.

If the sentry status is good, the function tries to generate whatever data is appropriate for the type of the argument.

If an exception is thrown during insertion, ios_base::badbit will be turned on in the stream's error state. If badbit is on in the stream's exceptions mask, the exception will be rethrown without completing its actions.

__ostream_typeput (char_type __c)
 
__ostream_typewrite (const char_type *__s, streamsize __n)
 

Protected Member Functions

template<typename _ValueT >
__ostream_type_M_insert (_ValueT __v)
 
void swap (basic_ostream &__rhs)
 

Detailed Description

template<typename _CharT, typename _Traits>
class std::basic_ofstream< _CharT, _Traits >

Controlling output for files.

Template Parameters
_CharTType of character stream.
_TraitsTraits for character type, defaults to char_traits<_CharT>.

This class supports reading from named files, using the inherited functions from std::basic_ostream. To control the associated sequence, an instance of std::basic_filebuf is used, which this page refers to as sb.

Definition at line 760 of file fstream.

Constructor & Destructor Documentation

◆ basic_ofstream() [1/4]

template<typename _CharT , typename _Traits >
std::basic_ofstream< _CharT, _Traits >::basic_ofstream ( )
inline

Default constructor.

Initializes sb using its default constructor, and passes &sb to the base class initializer. Does not open any files (you haven't given it a filename to open).

Definition at line 786 of file fstream.

◆ basic_ofstream() [2/4]

template<typename _CharT , typename _Traits >
std::basic_ofstream< _CharT, _Traits >::basic_ofstream ( const char *  __s,
ios_base::openmode  __mode = ios_base::out 
)
inlineexplicit

Create an output file stream.

Parameters
__sNull terminated string specifying the filename.
__modeOpen file in specified mode (see std::ios_base).

ios_base::out is automatically included in __mode.

Definition at line 797 of file fstream.

◆ basic_ofstream() [3/4]

template<typename _CharT , typename _Traits >
std::basic_ofstream< _CharT, _Traits >::basic_ofstream ( const std::string __s,
ios_base::openmode  __mode = ios_base::out 
)
inlineexplicit

Create an output file stream.

Parameters
__sstd::string specifying the filename.
__modeOpen file in specified mode (see std::ios_base).

ios_base::out is automatically included in __mode.

Definition at line 832 of file fstream.

◆ basic_ofstream() [4/4]

template<typename _CharT , typename _Traits >
template<typename _Path , typename _Require = _If_fs_path<_Path>>
std::basic_ofstream< _CharT, _Traits >::basic_ofstream ( const _Path &  __s,
ios_base::openmode  __mode = ios_base::out 
)
inline

Create an output file stream.

Parameters
__sfilesystem::path specifying the filename.
__modeOpen file in specified mode (see std::ios_base).

ios_base::out is automatically included in __mode.

Definition at line 849 of file fstream.

◆ ~basic_ofstream()

template<typename _CharT , typename _Traits >
std::basic_ofstream< _CharT, _Traits >::~basic_ofstream ( )
inline

The destructor does nothing.

The file is closed by the filebuf object, not the formatting stream.

Definition at line 869 of file fstream.

Member Function Documentation

◆ close()

template<typename _CharT , typename _Traits >
void std::basic_ofstream< _CharT, _Traits >::close ( )
inline

Close the file.

Calls std::basic_filebuf::close(). If that function fails, failbit is set in the stream's error state.

Definition at line 1000 of file fstream.

◆ flush()

template<typename _CharT , typename _Traits >
basic_ostream< _CharT, _Traits > & std::basic_ostream< _CharT, _Traits >::flush
inherited

Synchronizing the stream buffer.

Returns
*this

If rdbuf() is a null pointer, changes nothing.

Otherwise, calls rdbuf()->pubsync(), and if that returns -1, sets badbit.

Definition at line 222 of file ostream.tcc.

◆ is_open()

template<typename _CharT , typename _Traits >
bool std::basic_ofstream< _CharT, _Traits >::is_open ( )
inline

Wrapper to test for an open file.

Returns
rdbuf()->is_open()

Definition at line 910 of file fstream.

◆ open() [1/3]

template<typename _CharT , typename _Traits >
template<typename _Path >
_If_fs_path<_Path, void> std::basic_ofstream< _CharT, _Traits >::open ( const _Path &  __s,
ios_base::openmode  __mode = ios_base::out 
)
inline

Opens an external file.

Parameters
__sThe name of the file, as a filesystem::path.
__modeThe open mode flags.

Calls std::basic_filebuf::open(__s,__mode|out). If that function fails, failbit is set in the stream's error state.

Definition at line 988 of file fstream.

◆ open() [2/3]

template<typename _CharT , typename _Traits >
void std::basic_ofstream< _CharT, _Traits >::open ( const char *  __s,
ios_base::openmode  __mode = ios_base::out 
)
inline

Opens an external file.

Parameters
__sThe name of the file.
__modeThe open mode flags.

Calls std::basic_filebuf::open(__s,__mode|out). If that function fails, failbit is set in the stream's error state.

Definition at line 928 of file fstream.

◆ open() [3/3]

template<typename _CharT , typename _Traits >
void std::basic_ofstream< _CharT, _Traits >::open ( const std::string __s,
ios_base::openmode  __mode = ios_base::out 
)
inline

Opens an external file.

Parameters
__sThe name of the file.
__modeThe open mode flags.

Calls std::basic_filebuf::open(s,mode|out). If that function fails, failbit is set in the stream's error state.

Definition at line 967 of file fstream.

◆ operator<<() [1/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( __ios_type &(*)(__ios_type &)  __pf)
inlineinherited

Interface for manipulators.

Manipulators such as std::endl and std::hex use these functions in constructs like "std::cout << std::endl". For more information, see the iomanip header.

Definition at line 119 of file ostream.

◆ operator<<() [2/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( __ostream_type &(*)(__ostream_type &)  __pf)
inlineinherited

Interface for manipulators.

Manipulators such as std::endl and std::hex use these functions in constructs like "std::cout << std::endl". For more information, see the iomanip header.

Definition at line 110 of file ostream.

◆ operator<<() [3/17]

template<typename _CharT , typename _Traits >
basic_ostream< _CharT, _Traits > & std::basic_ostream< _CharT, _Traits >::operator<< ( __streambuf_type __sb)
inherited

Extracting from another streambuf.

Parameters
__sbA pointer to a streambuf

This function behaves like one of the basic arithmetic extractors, in that it also constructs a sentry object and has the same error handling behavior.

If __sb is NULL, the stream will set failbit in its error state.

Characters are extracted from __sb and inserted into *this until one of the following occurs:

  • the input stream reaches end-of-file,
  • insertion into the output sequence fails (in this case, the character that would have been inserted is not extracted), or
  • an exception occurs while getting a character from __sb, which sets failbit in the error state

If the function inserts no characters, failbit is set.

Definition at line 125 of file ostream.tcc.

◆ operator<<() [4/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( bool  __n)
inlineinherited

Integer arithmetic inserters.

Parameters
__nA variable of builtin integral type.
Returns
*this if successful

These functions use the stream's current locale (specifically, the num_get facet) to perform numeric formatting.

Definition at line 176 of file ostream.

◆ operator<<() [5/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( const void *  __p)
inlineinherited

Pointer arithmetic inserters.

Parameters
__pA variable of pointer type.
Returns
*this if successful

These functions use the stream's current locale (specifically, the num_get facet) to perform numeric formatting.

Definition at line 292 of file ostream.

◆ operator<<() [6/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( double  __f)
inlineinherited

Floating point arithmetic inserters.

Parameters
__fA variable of builtin floating point type.
Returns
*this if successful

These functions use the stream's current locale (specifically, the num_get facet) to perform numeric formatting.

Definition at line 222 of file ostream.

◆ operator<<() [7/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( float  __f)
inlineinherited

Floating point arithmetic inserters.

Parameters
__fA variable of builtin floating point type.
Returns
*this if successful

These functions use the stream's current locale (specifically, the num_get facet) to perform numeric formatting.

Definition at line 226 of file ostream.

◆ operator<<() [8/17]

template<typename _CharT , typename _Traits >
basic_ostream< _CharT, _Traits > & std::basic_ostream< _CharT, _Traits >::operator<< ( int  __n)
inherited

Integer arithmetic inserters.

Parameters
__nA variable of builtin integral type.
Returns
*this if successful

These functions use the stream's current locale (specifically, the num_get facet) to perform numeric formatting.

Definition at line 111 of file ostream.tcc.

◆ operator<<() [9/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( ios_base &(*)(ios_base &)  __pf)
inlineinherited

Interface for manipulators.

Manipulators such as std::endl and std::hex use these functions in constructs like "std::cout << std::endl". For more information, see the iomanip header.

Definition at line 129 of file ostream.

◆ operator<<() [10/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( long  __n)
inlineinherited

Integer arithmetic inserters.

Parameters
__nA variable of builtin integral type.
Returns
*this if successful

These functions use the stream's current locale (specifically, the num_get facet) to perform numeric formatting.

Definition at line 168 of file ostream.

◆ operator<<() [11/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( long double  __f)
inlineinherited

Floating point arithmetic inserters.

Parameters
__fA variable of builtin floating point type.
Returns
*this if successful

These functions use the stream's current locale (specifically, the num_get facet) to perform numeric formatting.

Definition at line 234 of file ostream.

◆ operator<<() [12/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( long long  __n)
inlineinherited

Integer arithmetic inserters.

Parameters
__nA variable of builtin integral type.
Returns
*this if successful

These functions use the stream's current locale (specifically, the num_get facet) to perform numeric formatting.

Definition at line 203 of file ostream.

◆ operator<<() [13/17]

template<typename _CharT , typename _Traits >
basic_ostream< _CharT, _Traits > & std::basic_ostream< _CharT, _Traits >::operator<< ( short  __n)
inherited

Integer arithmetic inserters.

Parameters
__nA variable of builtin integral type.
Returns
*this if successful

These functions use the stream's current locale (specifically, the num_get facet) to perform numeric formatting.

Definition at line 97 of file ostream.tcc.

◆ operator<<() [14/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( unsigned int  __n)
inlineinherited

Integer arithmetic inserters.

Parameters
__nA variable of builtin integral type.
Returns
*this if successful

These functions use the stream's current locale (specifically, the num_get facet) to perform numeric formatting.

Definition at line 194 of file ostream.

◆ operator<<() [15/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( unsigned long  __n)
inlineinherited

Integer arithmetic inserters.

Parameters
__nA variable of builtin integral type.
Returns
*this if successful

These functions use the stream's current locale (specifically, the num_get facet) to perform numeric formatting.

Definition at line 172 of file ostream.

◆ operator<<() [16/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( unsigned long long  __n)
inlineinherited

Integer arithmetic inserters.

Parameters
__nA variable of builtin integral type.
Returns
*this if successful

These functions use the stream's current locale (specifically, the num_get facet) to perform numeric formatting.

Definition at line 207 of file ostream.

◆ operator<<() [17/17]

template<typename _CharT , typename _Traits >
__ostream_type& std::basic_ostream< _CharT, _Traits >::operator<< ( unsigned short  __n)
inlineinherited

Integer arithmetic inserters.

Parameters
__nA variable of builtin integral type.
Returns
*this if successful

These functions use the stream's current locale (specifically, the num_get facet) to perform numeric formatting.

Definition at line 183 of file ostream.

◆ put()

template<typename _CharT , typename _Traits >
basic_ostream< _CharT, _Traits > & std::basic_ostream< _CharT, _Traits >::put ( char_type  __c)
inherited

Simple insertion.

Parameters
__cThe character to insert.
Returns
*this

Tries to insert __c.

Note
This function is not overloaded on signed char and unsigned char.

Definition at line 154 of file ostream.tcc.

◆ rdbuf()

template<typename _CharT , typename _Traits >
__filebuf_type* std::basic_ofstream< _CharT, _Traits >::rdbuf ( ) const
inline

Accessing the underlying buffer.

Returns
The current basic_filebuf buffer.

This hides both signatures of std::basic_ios::rdbuf().

Definition at line 902 of file fstream.

◆ seekp() [1/2]

template<typename _CharT , typename _Traits >
basic_ostream< _CharT, _Traits > & std::basic_ostream< _CharT, _Traits >::seekp ( off_type  __off,
ios_base::seekdir  __dir 
)
inherited

Changing the current write position.

Parameters
__offA file offset object.
__dirThe direction in which to seek.
Returns
*this

If fail() is not true, calls rdbuf()->pubseekoff(off,dir). If that function fails, sets failbit.

Definition at line 288 of file ostream.tcc.

◆ seekp() [2/2]

template<typename _CharT , typename _Traits >
basic_ostream< _CharT, _Traits > & std::basic_ostream< _CharT, _Traits >::seekp ( pos_type  __pos)
inherited

Changing the current write position.

Parameters
__posA file position object.
Returns
*this

If fail() is not true, calls rdbuf()->pubseekpos(pos). If that function fails, sets failbit.

Definition at line 269 of file ostream.tcc.

◆ tellp()

template<typename _CharT , typename _Traits >
basic_ostream< _CharT, _Traits >::pos_type std::basic_ostream< _CharT, _Traits >::tellp
inherited

Getting the current write position.

Returns
A file position object.

If fail() is not false, returns pos_type(-1) to indicate failure. Otherwise returns rdbuf()->pubseekoff(0,cur,out).

Definition at line 257 of file ostream.tcc.

◆ write()

template<typename _CharT , typename _Traits >
basic_ostream< _CharT, _Traits > & std::basic_ostream< _CharT, _Traits >::write ( const char_type *  __s,
streamsize  __n 
)
inherited

Character string insertion.

Parameters
__sThe array to insert.
__nMaximum number of characters to insert.
Returns
*this

Characters are copied from __s and inserted into the stream until one of the following happens:

  • __n characters are inserted
  • inserting into the output sequence fails (in this case, badbit will be set in the stream's error state)
Note
This function is not overloaded on signed char and unsigned char.

Definition at line 188 of file ostream.tcc.


The documentation for this class was generated from the following file: