STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions
I/O

Functions

namespace std _GLIBCXX_VISIBILITY (default)
 

Detailed Description

Nearly all of the I/O classes are parameterized on the type of characters they read and write. (The major exception is ios_base at the top of the hierarchy.) This is a change from pre-Standard streams, which were not templates.

For ease of use and compatibility, all of the basic_* I/O-related classes are given typedef names for both of the builtin character widths (wide and narrow). The typedefs are the same as the pre-Standard names, for example:

typedef basic_ifstream<char> ifstream;

Because properly forward-declaring these classes can be difficult, you should not do it yourself. Instead, include the <iosfwd> header, which contains only declarations of all the I/O classes as well as the typedefs. Trying to forward-declare the typedefs themselves (e.g., class ostream;) is not valid ISO C++.

For more specific declarations, see http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch24.html

Function Documentation

namespace std _GLIBCXX_VISIBILITY ( default  )

Template class basic_ios, virtual base class for all stream classes.

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

Most of the member functions called dispatched on stream objects (e.g., std::cout.foo(bar);) are consolidated in this class.

These are standard types. They permit a standardized way of referring to names of (or names dependent on) the template parameters, which are specific to the implementation.

These are non-standard types.

The quick-and-easy status check.

This allows you to write constructs such as if (!a_stream) ... and while (a_stream) ...

Returns the error state of the stream buffer.

Returns
A bit pattern (well, isn't everything?)

See std::ios_base::iostate for the possible bit values. Most users will call one of the interpreting wrappers, e.g., good().

[Re]sets the error state.

Parameters
__stateThe new state flag(s) to set.

See std::ios_base::iostate for the possible bit values. Most users will not need to pass an argument.

Sets additional flags in the error state.

Parameters
__stateThe additional state flag(s) to set.

See std::ios_base::iostate for the possible bit values.

Fast error checking.

Returns
True if no error flags are set.

A wrapper around rdstate.

Fast error checking.

Returns
True if the eofbit is set.

Note that other iostate flags may also be set.

Fast error checking.

Returns
True if either the badbit or the failbit is set.

Checking the badbit in fail() is historical practice. Note that other iostate flags may also be set.

Fast error checking.

Returns
True if the badbit is set.

Note that other iostate flags may also be set.

Throwing exceptions on errors.

Returns
The current exceptions mask.

This changes nothing in the stream. See the one-argument version of exceptions(iostate) for the meaning of the return value.

Throwing exceptions on errors.

Parameters
__exceptThe new exceptions mask.

By default, error flags are set silently. You can set an exceptions mask for each stream; if a bit in the mask becomes set in the error flags, then an exception of type std::ios_base::failure is thrown.

If the error flag is already set when the exceptions mask is added, the exception is immediately thrown. Try running the following under GCC 3.1 or later:

#include <iostream>
#include <fstream>
#include <exception>
int main()
{
std::ifstream f ("/etc/motd");
std::cerr << "Setting badbit\n";
f.setstate (std::ios_base::badbit);
std::cerr << "Setting exception mask\n";
f.exceptions (std::ios_base::badbit);
}

Constructor performs initialization.

The parameter is passed by derived streams.

Empty.

The destructor does nothing. More specifically, it does not destroy the streambuf held by rdbuf().

Fetches the current tied stream.

Returns
A pointer to the tied stream, or NULL if the stream is not tied.

A stream may be tied (or synchronized) to a second output stream. When this stream performs any I/O, the tied stream is first flushed. For example, std::cin is tied to std::cout.

Ties this stream to an output stream.

Parameters
__tiestrThe output stream.
Returns
The previously tied output stream, or NULL if the stream was not tied.

This sets up a new tie; see tie() for more.

Accessing the underlying buffer.

Returns
The current stream buffer.

This does not change the state of the stream.

Changing the underlying buffer.

Parameters
__sbThe new stream buffer.
Returns
The previous stream buffer.

Associates a new buffer with the current stream, and clears the error state.

Due to historical accidents which the LWG refuses to correct, the I/O library suffers from a design error: this function is hidden in derived classes by overrides of the zero-argument rdbuf(), which is non-virtual for hysterical raisins. As a result, you must use explicit qualifications to access this function via any derived class. For example:

std::fstream foo; // or some other derived type
std::streambuf* p = .....;
foo.ios::rdbuf(p); // ios == basic_ios<char>

Copies fields of __rhs into this.

Parameters
__rhsThe source values for the copies.
Returns
Reference to this object.

All fields of __rhs are copied into this object except that rdbuf() and rdstate() remain unchanged. All values in the pword and iword arrays are copied. Before copying, each callback is invoked with erase_event. After copying, each (new) callback is invoked with copyfmt_event. The final step is to copy exceptions().

Retrieves the empty character.

Returns
The current fill character.

It defaults to a space (' ') in the current locale.

Sets a new empty character.

Parameters
__chThe new character.
Returns
The previous fill character.

The fill character is used to fill out space when P+ characters have been requested (e.g., via setw), Q characters are actually used, and Q<P. It defaults to a space (' ') in the current locale.

Moves to a new locale.

Parameters
__locThe new locale.
Returns
The previous locale.

Calls ios_base::imbue(loc), and if a stream buffer is associated with this stream, calls that buffer's pubimbue(loc).

Additional l10n notes are at http://gcc.gnu.org/onlinedocs/libstdc++/manual/localization.html

Squeezes characters.

Parameters
__cThe character to narrow.
__dfaultThe character to narrow.
Returns
The narrowed character.

Maps a character of char_type to a character of char, if possible.

Returns the result of

std::use_facet<ctype<char_type> >(getloc()).narrow(c,dfault)

Additional l10n notes are at http://gcc.gnu.org/onlinedocs/libstdc++/manual/localization.html

Widens characters.

Parameters
__cThe character to widen.
Returns
The widened character.

Maps a character of char to a character of char_type.

Returns the result of

std::use_facet<ctype<char_type> >(getloc()).widen(c)

Additional l10n notes are at http://gcc.gnu.org/onlinedocs/libstdc++/manual/localization.html

Empty.

The default constructor does nothing and is not normally accessible to users.

All setup is performed here.

This is called from the public constructor. It is not virtual and cannot be redefined.

The base of the I/O class hierarchy.

This class defines everything that can be defined about I/O that does not depend on the type of characters being input or output. Most people will only see ios_base when they need to specify the full name of the various I/O flags (e.g., the openmodes).

These are thrown to indicate problems with io.

27.4.2.1.1 Class ios_base::failure

This is a bitmask type.

_Ios_Fmtflags is implementation-defined, but it is valid to perform bitwise operations on these values and expect the Right Thing to happen. Defined objects of type fmtflags are:

  • boolalpha
  • dec
  • fixed
  • hex
  • internal
  • left
  • oct
  • right
  • scientific
  • showbase
  • showpoint
  • showpos
  • skipws
  • unitbuf
  • uppercase
  • adjustfield
  • basefield
  • floatfield

Insert/extract bool in alphabetic rather than numeric format.

Converts integer input or generates integer output in decimal base.

Generate floating-point output in fixed-point notation.

Converts integer input or generates integer output in hexadecimal base.

Adds fill characters at a designated internal point in certain generated output, or identical to right if no such point is designated.

Adds fill characters on the right (final positions) of certain generated output. (I.e., the thing you print is flush left.)

Converts integer input or generates integer output in octal base.

Adds fill characters on the left (initial positions) of certain generated output. (I.e., the thing you print is flush right.)

Generates floating-point output in scientific notation.

Generates a prefix indicating the numeric base of generated integer output.

Generates a decimal-point character unconditionally in generated floating-point output.

Generates a + sign in non-negative generated numeric output.

Skips leading white space before certain input operations.

Flushes output after each output operation.

Replaces certain lowercase letters with their uppercase equivalents in generated output.

A mask of left|right|internal. Useful for the 2-arg form of setf.

A mask of dec|oct|hex. Useful for the 2-arg form of setf.

A mask of scientific|fixed. Useful for the 2-arg form of setf.

This is a bitmask type.

_Ios_Iostate is implementation-defined, but it is valid to perform bitwise operations on these values and expect the Right Thing to happen. Defined objects of type iostate are:

  • badbit
  • eofbit
  • failbit
  • goodbit

Indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error from a file).

Indicates that an input operation reached the end of an input sequence.

Indicates that an input operation failed to read the expected characters, or that an output operation failed to generate the desired characters.

Indicates all is well.

This is a bitmask type.

_Ios_Openmode is implementation-defined, but it is valid to perform bitwise operations on these values and expect the Right Thing to happen. Defined objects of type openmode are:

  • app
  • ate
  • binary
  • in
  • out
  • trunc

Seek to end before each write.

Open and seek to end immediately after opening.

Perform input and output in binary mode (as opposed to text mode). This is probably not what you think it is; see http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch27s02.html

Open for input. Default for ifstream and fstream.

Open for output. Default for ofstream and fstream.

Open for input. Default for ofstream.

This is an enumerated type.

_Ios_Seekdir is implementation-defined. Defined values of type seekdir are:

  • beg
  • cur, equivalent to SEEK_CUR in the C standard library.
  • end, equivalent to SEEK_END in the C standard library.

Request a seek relative to the beginning of the stream.

Request a seek relative to the current position within the sequence.

Request a seek relative to the current end of the sequence.

The set of events that may be passed to an event callback.

erase_event is used during ~ios() and copyfmt(). imbue_event is used during imbue(). copyfmt_event is used during copyfmt().

The type of an event callback function.

Parameters
__eOne of the members of the event enum.
__bReference to the ios_base object.
__iThe integer provided when the callback was registered.

Event callbacks are user defined functions that get called during several ios_base and basic_ios functions, specifically imbue(), copyfmt(), and ~ios().

Add the callback __fn with parameter __index.

Parameters
__fnThe function to add.
__indexThe integer to pass to the function when invoked.

Registers a function as an event callback with an integer parameter to be passed to the function when invoked. Multiple copies of the function are allowed. If there are multiple callbacks, they are invoked in the order they were registered.

Access to format flags.

Returns
The format control flags for both input and output.

Setting new format flags all at once.

Parameters
__fmtflThe new flags to set.
Returns
The previous format control flags.

This function overwrites all the format flags with __fmtfl.

Setting new format flags.

Parameters
__fmtflAdditional flags to set.
Returns
The previous format control flags.

This function sets additional flags in format control. Flags that were previously set remain set.

Setting new format flags.

Parameters
__fmtflAdditional flags to set.
__maskThe flags mask for fmtfl.
Returns
The previous format control flags.

This function clears mask in the format flags, then sets fmtfl & mask. An example mask is ios_base::adjustfield.

Clearing format flags.

Parameters
__maskThe flags to unset.

This function clears __mask in the format flags.

Flags access.

Returns
The precision to generate on certain output operations.

Be careful if you try to give a definition of precision here; see DR 189.

Changing flags.

Parameters
__precThe new precision value.
Returns
The previous value of precision().

Flags access.

Returns
The minimum field width to generate on output operations.

Minimum field width refers to the number of characters.

Changing flags.

Parameters
__wideThe new width value.
Returns
The previous value of width().

Interaction with the standard C I/O objects.

Parameters
__syncWhether to synchronize or not.
Returns
True if the standard streams were previously synchronized.

The synchronization referred to is only that between the standard C facilities (e.g., stdout) and the standard C++ objects (e.g., cout). User-declared streams are unaffected. See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch28s02.html

Setting a new locale.

Parameters
__locThe new locale.
Returns
The previous locale.

Sets the new locale for this stream, and then invokes each callback with imbue_event.

Locale access

Returns
A copy of the current locale.

If imbue(loc) has previously been called, then this function returns loc. Otherwise, it returns a copy of std::locale(), the global C++ locale.

Locale access

Returns
A reference to the current locale.

Like getloc above, but returns a reference instead of generating a copy.

Access to unique indices.

Returns
An integer different from all previous calls.

This function returns a unique integer every time it is called. It can be used for any purpose, but is primarily intended to be a unique index for the iword and pword functions. The expectation is that an application calls xalloc in order to obtain an index in the iword and pword arrays that can be used without fear of conflict.

The implementation maintains a static variable that is incremented and returned on each invocation. xalloc is guaranteed to return an index that is safe to use in the iword and pword arrays.

Access to integer array.

Parameters
__ixIndex into the array.
Returns
A reference to an integer associated with the index.

The iword function provides access to an array of integers that can be used for any purpose. The array grows as required to hold the supplied index. All integers in the array are initialized to 0.

The implementation reserves several indices. You should use xalloc to obtain an index that is safe to use. Also note that since the array can grow dynamically, it is not safe to hold onto the reference.

Access to void pointer array.

Parameters
__ixIndex into the array.
Returns
A reference to a void* associated with the index.

The pword function provides access to an array of pointers that can be used for any purpose. The array grows as required to hold the supplied index. All pointers in the array are initialized to 0.

The implementation reserves several indices. You should use xalloc to obtain an index that is safe to use. Also note that since the array can grow dynamically, it is not safe to hold onto the reference.

Invokes each callback with erase_event. Destroys local storage.

Note that the ios_base object for the standard streams never gets destroyed. As a result, any callbacks registered with the standard streams will not get invoked with erase_event (unless copyfmt is used).

Calls base.setf(ios_base::boolalpha).

Calls base.unsetf(ios_base::boolalpha).

Calls base.setf(ios_base::showbase).

Calls base.unsetf(ios_base::showbase).

Calls base.setf(ios_base::showpoint).

Calls base.unsetf(ios_base::showpoint).

Calls base.setf(ios_base::showpos).

Calls base.unsetf(ios_base::showpos).

Calls base.setf(ios_base::skipws).

Calls base.unsetf(ios_base::skipws).

Calls base.setf(ios_base::uppercase).

Calls base.unsetf(ios_base::uppercase).

Calls base.setf(ios_base::unitbuf).

Calls base.unsetf(ios_base::unitbuf).

Calls base.setf(ios_base::internal, ios_base::adjustfield).

Calls base.setf(ios_base::left, ios_base::adjustfield).

Calls base.setf(ios_base::right, ios_base::adjustfield).

Calls base.setf(ios_base::dec, ios_base::basefield).

Calls base.setf(ios_base::hex, ios_base::basefield).

Calls base.setf(ios_base::oct, ios_base::basefield).

Calls base.setf(ios_base::fixed, ios_base::floatfield).

Calls base.setf(ios_base::scientific, ios_base::floatfield).

Provides a layer of compatibility for C/POSIX.

This GNU extension provides extensions for working with standard C FILE*'s and POSIX file descriptors. It must be instantiated by the user with the type of character used in the file stream, e.g., stdio_filebuf<char>.

deferred initialization

Parameters
__fdAn open file descriptor.
__modeSame meaning as in a standard filebuf.
__sizeOptimal or preferred size of internal buffer, in chars.

This constructor associates a file stream buffer with an open POSIX file descriptor. The file descriptor will be automatically closed when the stdio_filebuf is closed/destroyed.

Parameters
__fAn open FILE*.
__modeSame meaning as in a standard filebuf.
__sizeOptimal or preferred size of internal buffer, in chars. Defaults to system's BUFSIZ.

This constructor associates a file stream buffer with an open C FILE*. The FILE* will not be automatically closed when the stdio_filebuf is closed/destroyed.

Closes the external data stream if the file descriptor constructor was used.

Returns
The underlying file descriptor.

Once associated with an external data stream, this function can be used to access the underlying POSIX file descriptor. Note that there is no way for the library to track what you do with the descriptor, so be careful.

Returns
The underlying FILE*.

This function can be used to access the underlying "C" file pointer. Note that there is no way for the library to track what you do with the file, so be careful.

Provides a layer of compatibility for C.

This GNU extension provides extensions for working with standard C FILE*'s. It must be instantiated by the user with the type of character used in the file stream, e.g., stdio_filebuf<char>.

Returns
The underlying FILE*.

This function can be used to access the underlying C file pointer. Note that there is no way for the library to track what you do with the file, so be careful.

The actual work of input and output (for files).

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

This class associates both its input and output sequence with an external disk file, and maintains a joint file position for both sequences. Many of its semantics are described in terms of similar behavior in the Standard C Library's FILE streams.

Requirements on traits_type, specific to this class:

  • traits_type::pos_type must be fpos<traits_type::state_type>
  • traits_type::off_type must be streamoff
  • traits_type::state_type must be Assignable and DefaultConstructible,
  • traits_type::state_type() must be the initial state for codecvt.

Place to stash in || out || in | out settings for current filebuf.

Pointer to the beginning of internal buffer.

Actual size of internal buffer. This number is equal to the size of the put area + 1 position, reserved for the overflow char of a full area.

_M_reading == false && _M_writing == false for uncommitted mode; _M_reading == true for read mode; _M_writing == true for write mode;

NB: _M_reading == true && _M_writing == true is unused.

Necessary bits for putback buffer management.

Note
pbacks of over one character are not currently supported.

Buffer for external characters. Used for input when codecvt::always_noconv() == false. When valid, this corresponds to eback().

Size of buffer held by _M_ext_buf.

Pointers into the buffer held by _M_ext_buf that delimit a subsequence of bytes that have been read but not yet converted. When valid, _M_ext_next corresponds to egptr().

Initializes pback buffers, and moves normal buffers to safety. Assumptions: _M_in_cur has already been moved back

Deactivates pback buffer contents, and restores normal buffer. Assumptions: The pback buffer has only moved forward.

Does not open any files.

The default constructor initializes the parent class using its own default ctor.

The destructor closes the file first.

Returns true if the external file is open.

Opens an external file.

Parameters
__sThe name of the file.
__modeThe open mode flags.
Returns
this on success, NULL on failure

If a file is already open, this function immediately fails. Otherwise it tries to open the file named __s using the flags given in __mode.

Table 92, adapted here, gives the relation between openmode combinations and the equivalent fopen() flags. (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app, and binary|in|app per DR 596) +———————————————————+ | ios_base Flag combination stdio equivalent | |binary in out trunc app | +———————————————————+ | + w | | + + a | | + a | | + + w | | + r | | + + r+ | | + + + w+ | | + + + a+ | | + + a+ | +———————————————————+ | + + wb | | + + + ab | | + + ab | | + + + wb | | + + rb | | + + + r+b | | + + + + w+b | | + + + + a+b | | + + + a+b | +———————————————————+

Closes the currently associated file.

Returns
this on success, NULL on failure

If no file is currently open, this function immediately fails.

If a put buffer area exists, overflow(eof) is called to flush all the characters. The file is then closed.

If any operations fail, this function also fails.

Manipulates the buffer.

Parameters
__sPointer to a buffer area.
__nSize of __s.
Returns
this

If no file has been opened, and both __s and __n are zero, then the stream becomes unbuffered. Otherwise, __s is used as a buffer; see http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html for more.

This function sets the pointers of the internal buffer, both get and put areas. Typically:

__off == egptr() - eback() upon underflow/uflow (read mode); __off == 0 upon overflow (write mode); __off == -1 upon open, setbuf, seekoff/pos (uncommitted mode).

NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size reflects the actual allocated memory and the last cell is reserved for the overflow char of a full put area.

Controlling input 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_istream. To control the associated sequence, an instance of std::basic_filebuf is used, which this page refers to as sb.

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

Create an input file stream.

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

ios_base::in is automatically included in __mode.

Tip: When using std::string to hold the filename, you must use .c_str() before passing it to this constructor.

The destructor does nothing.

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

Accessing the underlying buffer.

Returns
The current basic_filebuf buffer.

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

Wrapper to test for an open file.

Returns
rdbuf()->is_open()

Opens an external file.

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

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

Tip: When using std::string to hold the filename, you must use .c_str() before passing it to this constructor.

Close the file.

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

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.

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

Create an output file stream.

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

ios_base::out | ios_base::trunc is automatically included in __mode.

Tip: When using std::string to hold the filename, you must use .c_str() before passing it to this constructor.

The destructor does nothing.

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

Accessing the underlying buffer.

Returns
The current basic_filebuf buffer.

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

Wrapper to test for an open file.

Returns
rdbuf()->is_open()

Opens an external file.

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

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

Tip: When using std::string to hold the filename, you must use .c_str() before passing it to this constructor.

Close the file.

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

Controlling input and output for files.

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

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

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

Create an input/output file stream.

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

Tip: When using std::string to hold the filename, you must use .c_str() before passing it to this constructor.

The destructor does nothing.

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

Accessing the underlying buffer.

Returns
The current basic_filebuf buffer.

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

Wrapper to test for an open file.

Returns
rdbuf()->is_open()

Opens an external file.

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

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

Tip: When using std::string to hold the filename, you must use .c_str() before passing it to this constructor.

Close the file.

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

The actual work of input and output (for std::string).

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

This class associates either or both of its input and output sequences with a sequence of characters, which can be initialized from, or made available as, a std::basic_string. (Paraphrased from [27.7.1]/1.)

For this class, open modes (of type ios_base::openmode) have in set if the input sequence can be read, and out set if the output sequence can be written.

Place to stash in || out || in | out settings for current stringbuf.

Starts with an empty string buffer.

Parameters
__modeWhether the buffer can read, or write, or both.

The default constructor initializes the parent class using its own default ctor.

Starts with an existing string buffer.

Parameters
__strA string to copy as a starting buffer.
__modeWhether the buffer can read, or write, or both.

This constructor initializes the parent class using its own default ctor.

Copying out the string buffer.

Returns
A copy of one of the underlying sequences.

If the buffer is only created in input mode, the underlying character sequence is equal to the input sequence; otherwise, it is equal to the output sequence. [27.7.1.2]/1

Setting a new buffer.

Parameters
__sThe string to use as a new sequence.

Deallocates any previous stored sequence, then copies s to use as a new one.

Manipulates the buffer.

Parameters
__sPointer to a buffer area.
__nSize of __s.
Returns
this

If no buffer has already been created, and both __s and __n are non-zero, then __s is used as a buffer; see http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html for more.

Controlling input for std::string.

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

This class supports reading from objects of type std::basic_string, using the inherited functions from std::basic_istream. To control the associated sequence, an instance of std::basic_stringbuf is used, which this page refers to as sb.

Default constructor starts with an empty string buffer.

Parameters
__modeWhether the buffer can read, or write, or both.

ios_base::in is automatically included in __mode.

Initializes sb using __mode|in, and passes &sb to the base class initializer. Does not allocate any buffer.

That's a lie. We initialize the base class with NULL, because the string class does its own memory management.

Starts with an existing string buffer.

Parameters
__strA string to copy as a starting buffer.
__modeWhether the buffer can read, or write, or both.

ios_base::in is automatically included in mode.

Initializes sb using str and mode|in, and passes &sb to the base class initializer.

That's a lie. We initialize the base class with NULL, because the string class does its own memory management.

The destructor does nothing.

The buffer is deallocated by the stringbuf object, not the formatting stream.

Accessing the underlying buffer.

Returns
The current basic_stringbuf buffer.

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

Copying out the string buffer.

Returns
rdbuf()->str()

Setting a new buffer.

Parameters
__sThe string to use as a new sequence.

Calls rdbuf()->str(s).

Controlling output for std::string.

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

This class supports writing to objects of type std::basic_string, using the inherited functions from std::basic_ostream. To control the associated sequence, an instance of std::basic_stringbuf is used, which this page refers to as sb.

Default constructor starts with an empty string buffer.

Parameters
__modeWhether the buffer can read, or write, or both.

ios_base::out is automatically included in mode.

Initializes sb using mode|out, and passes &sb to the base class initializer. Does not allocate any buffer.

That's a lie. We initialize the base class with NULL, because the string class does its own memory management.

Starts with an existing string buffer.

Parameters
__strA string to copy as a starting buffer.
__modeWhether the buffer can read, or write, or both.

ios_base::out is automatically included in mode.

Initializes sb using str and mode|out, and passes &sb to the base class initializer.

That's a lie. We initialize the base class with NULL, because the string class does its own memory management.

The destructor does nothing.

The buffer is deallocated by the stringbuf object, not the formatting stream.

Accessing the underlying buffer.

Returns
The current basic_stringbuf buffer.

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

Copying out the string buffer.

Returns
rdbuf()->str()

Setting a new buffer.

Parameters
__sThe string to use as a new sequence.

Calls rdbuf()->str(s).

Controlling input and output for std::string.

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

This class supports reading from and writing to objects of type std::basic_string, using the inherited functions from std::basic_iostream. To control the associated sequence, an instance of std::basic_stringbuf is used, which this page refers to as sb.

Default constructor starts with an empty string buffer.

Parameters
__mWhether the buffer can read, or write, or both.

Initializes sb using the mode from __m, and passes &sb to the base class initializer. Does not allocate any buffer.

That's a lie. We initialize the base class with NULL, because the string class does its own memory management.

Starts with an existing string buffer.

Parameters
__strA string to copy as a starting buffer.
__mWhether the buffer can read, or write, or both.

Initializes sb using __str and __m, and passes &sb to the base class initializer.

That's a lie. We initialize the base class with NULL, because the string class does its own memory management.

The destructor does nothing.

The buffer is deallocated by the stringbuf object, not the formatting stream.

Accessing the underlying buffer.

Returns
The current basic_stringbuf buffer.

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

Copying out the string buffer.

Returns
rdbuf()->str()

Setting a new buffer.

Parameters
__sThe string to use as a new sequence.

Calls rdbuf()->str(s).

The actual work of input and output (interface).

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

This is a base class. Derived stream buffers each control a pair of character sequences: one for input, and one for output.

Section [27.5.1] of the standard describes the requirements and behavior of stream buffer classes. That section (three paragraphs) is reproduced here, for simplicity and accuracy.

  1. Stream buffers can impose various constraints on the sequences they control. Some constraints are:
    • The controlled input sequence can be not readable.
    • The controlled output sequence can be not writable.
    • The controlled sequences can be associated with the contents of other representations for character sequences, such as external files.
    • The controlled sequences can support operations directly to or from associated sequences.
    • The controlled sequences can impose limitations on how the program can read characters from a sequence, write characters to a sequence, put characters back into an input sequence, or alter the stream position.
  2. Each sequence is characterized by three pointers which, if non-null, all point into the same charT array object. The array object represents, at any moment, a (sub)sequence of characters from the sequence. Operations performed on a sequence alter the values stored in these pointers, perform reads and writes directly to or from associated sequences, and alter the stream position and conversion state as needed to maintain this subsequence relationship. The three pointers are:
    • the beginning pointer, or lowest element address in the array (called xbeg here);
    • the next pointer, or next element address that is a current candidate for reading or writing (called xnext here);
    • the end pointer, or first element address beyond the end of the array (called xend here).
  3. The following semantic constraints shall always apply for any set of three pointers for a sequence, using the pointer names given immediately above:
    • If xnext is not a null pointer, then xbeg and xend shall also be non-null pointers into the same charT array, as described above; otherwise, xbeg and xend shall also be null.
    • If xnext is not a null pointer and xnext < xend for an output sequence, then a write position is available. In this case, *xnext shall be assignable as the next element to write (to put, or to store a character value, into the sequence).
    • If xnext is not a null pointer and xbeg < xnext for an input sequence, then a putback position is available. In this case, xnext[-1] shall have a defined value and is the next (preceding) element to store a character that is put back into the input sequence.
    • If xnext is not a null pointer and xnext< xend for an input sequence, then a read position is available. In this case, *xnext shall have a defined value and is the next element to read (to get, or to obtain a character value, from the sequence).

These are standard types. They permit a standardized way of referring to names of (or names dependent on) the template parameters, which are specific to the implementation.

This is a non-standard type.

< Start of get area.

< Current read area.

< End of get area.

< Start of put area.

< Current put area.

< End of put area.

Current locale setting.

Destructor deallocates no buffer space.

Entry point for imbue().

Parameters
__locThe new locale.
Returns
The previous locale.

Calls the derived imbue(__loc).

Locale access.

Returns
The current locale in effect.

If pubimbue(loc) has been called, then the most recent loc is returned. Otherwise the global locale in effect at the time of construction is returned.

Entry points for derived buffer functions.

The public versions of pubfoo dispatch to the protected derived foo member functions, passing the arguments (if any) and returning the result unchanged.

Alters the stream position.

Parameters
__offOffset.
__wayValue for ios_base::seekdir.
__modeValue for ios_base::openmode.

Calls virtual seekoff function.

Alters the stream position.

Parameters
__spPosition
__modeValue for ios_base::openmode.

Calls virtual seekpos function.

Calls virtual sync function.

Looking ahead into the stream.

Returns
The number of characters available.

If a read position is available, returns the number of characters available for reading before the buffer must be refilled. Otherwise returns the derived showmanyc().

Getting the next character.

Returns
The next character, or eof.

Calls sbumpc(), and if that function returns traits::eof(), so does this function. Otherwise, sgetc().

Getting the next character.

Returns
The next character, or eof.

If the input read position is available, returns that character and increments the read pointer, otherwise calls and returns uflow().

Getting the next character.

Returns
The next character, or eof.

If the input read position is available, returns that character, otherwise calls and returns underflow(). Does not move the read position after fetching the character.

Entry point for xsgetn.

Parameters
__sA buffer area.
__nA count.

Returns xsgetn(__s,__n). The effect is to fill __s[0] through __s[__n-1] with characters from the input sequence, if possible.

Pushing characters back into the input stream.

Parameters
__cThe character to push back.
Returns
The previous character, if possible.

Similar to sungetc(), but __c is pushed onto the stream instead of the previous character. If successful, the next character fetched from the input stream will be __c.

Moving backwards in the input stream.

Returns
The previous character, if possible.

If a putback position is available, this function decrements the input pointer and returns that character. Otherwise, calls and returns pbackfail(). The effect is to unget the last character gotten.

Entry point for all single-character output functions.

Parameters
__cA character to output.
Returns
__c, if possible.

One of two public output functions.

If a write position is available for the output sequence (i.e., the buffer is not full), stores __c in that position, increments the position, and returns traits::to_int_type(__c). If a write position is not available, returns overflow(__c).

Entry point for all single-character output functions.

Parameters
__sA buffer read area.
__nA count.

One of two public output functions.

Returns xsputn(__s,__n). The effect is to write __s[0] through __s[__n-1] to the output sequence, if possible.

Base constructor.

Only called from derived constructors, and sets up all the buffer data to zero, including the pointers described in the basic_streambuf class description. Note that, as a result,

  • the class starts with no read nor write positions available,
  • this is not an error

Access to the get area.

These functions are only available to other protected functions, including derived classes.

  • eback() returns the beginning pointer for the input sequence
  • gptr() returns the next pointer for the input sequence
  • egptr() returns the end pointer for the input sequence

Moving the read position.

Parameters
__nThe delta by which to move.

This just advances the read position without returning any data.

Setting the three read area pointers.

Parameters
__gbegA pointer.
__gnextA pointer.
__gendA pointer.
Postcondition
__gbeg == eback(), __gnext == gptr(), and __gend == egptr()

Access to the put area.

These functions are only available to other protected functions, including derived classes.

  • pbase() returns the beginning pointer for the output sequence
  • pptr() returns the next pointer for the output sequence
  • epptr() returns the end pointer for the output sequence

Moving the write position.

Parameters
__nThe delta by which to move.

This just advances the write position without returning any data.

Setting the three write area pointers.

Parameters
__pbegA pointer.
__pendA pointer.
Postcondition
__pbeg == pbase(), __pbeg == pptr(), and __pend == epptr()

Changes translations.

Parameters
__locA new locale.

Translations done during I/O which depend on the current locale are changed by this call. The standard adds, Between invocations of this function a class derived from streambuf can safely cache results of calls to locale functions and to members of facets so obtained.

Note
Base class version does nothing.

Manipulates the buffer.

Each derived class provides its own appropriate behavior. See the next-to-last paragraph of http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html for more on this function.

Note
Base class version does nothing, returns this.

Alters the stream positions.

Each derived class provides its own appropriate behavior.

Note
Base class version does nothing, returns a pos_type that represents an invalid stream position.

Alters the stream positions.

Each derived class provides its own appropriate behavior.

Note
Base class version does nothing, returns a pos_type that represents an invalid stream position.

Synchronizes the buffer arrays with the controlled sequences.

Returns
-1 on failure.

Each derived class provides its own appropriate behavior, including the definition of failure.

Note
Base class version does nothing, returns zero.

Investigating the data available.

Returns
An estimate of the number of characters available in the input sequence, or -1.

If it returns a positive value, then successive calls to underflow() will not return traits::eof() until at least that number of characters have been supplied. If showmanyc() returns -1, then calls to underflow() or uflow() will fail. [27.5.2.4.3]/1

Note
Base class version does nothing, returns zero.
The standard adds that the intention is not only that the calls [to underflow or uflow] will not return eof() but that they will return immediately.
The standard adds that the morphemes of showmanyc are es-how-many-see, not show-manic.

Multiple character extraction.

Parameters
__sA buffer area.
__nMaximum number of characters to assign.
Returns
The number of characters assigned.

Fills __s[0] through __s[__n-1] with characters from the input sequence, as if by sbumpc(). Stops when either __n characters have been copied, or when traits::eof() would be copied.

It is expected that derived classes provide a more efficient implementation by overriding this definition.

Fetches more data from the controlled sequence.

Returns
The first character from the pending sequence.

Informally, this function is called when the input buffer is exhausted (or does not exist, as buffering need not actually be done). If a buffer exists, it is refilled. In either case, the next available character is returned, or traits::eof() to indicate a null pending sequence.

For a formal definition of the pending sequence, see a good text such as Langer & Kreft, or [27.5.2.4.3]/7-14.

A functioning input streambuf can be created by overriding only this function (no buffer area will be used). For an example, see http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25.html

Note
Base class version does nothing, returns eof().

Fetches more data from the controlled sequence.

Returns
The first character from the pending sequence.

Informally, this function does the same thing as underflow(), and in fact is required to call that function. It also returns the new character, like underflow() does. However, this function also moves the read position forward by one.

Tries to back up the input sequence.

Parameters
__cThe character to be inserted back into the sequence.
Returns
eof() on failure, some other value on success
Postcondition
The constraints of gptr(), eback(), and pptr() are the same as for underflow().
Note
Base class version does nothing, returns eof().

Multiple character insertion.

Parameters
__sA buffer area.
__nMaximum number of characters to write.
Returns
The number of characters written.

Writes __s[0] through __s[__n-1] to the output sequence, as if by sputc(). Stops when either n characters have been copied, or when sputc() would return traits::eof().

It is expected that derived classes provide a more efficient implementation by overriding this definition.

Consumes data from the buffer; writes to the controlled sequence.

Parameters
__cAn additional character to consume.
Returns
eof() to indicate failure, something else (usually __c, or not_eof())

Informally, this function is called when the output buffer is full (or does not exist, as buffering need not actually be done). If a buffer exists, it is consumed, with some effect on the controlled sequence. (Typically, the buffer is written out to the sequence verbatim.) In either case, the character c is also written out, if __c is not eof().

For a formal definition of this function, see a good text such as Langer & Kreft, or [27.5.2.4.5]/3-7.

A functioning output streambuf can be created by overriding only this function (no buffer area will be used).

Note
Base class version does nothing, returns eof().
41 {
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 
44  template<typename _Facet>
45  inline const _Facet&
46  __check_facet(const _Facet* __f)
47  {
48  if (!__f)
49  __throw_bad_cast();
50  return *__f;
51  }
52 
65  template<typename _CharT, typename _Traits>
66  class basic_ios : public ios_base
67  {
68  public:
70 
75  typedef _CharT char_type;
76  typedef typename _Traits::int_type int_type;
77  typedef typename _Traits::pos_type pos_type;
78  typedef typename _Traits::off_type off_type;
79  typedef _Traits traits_type;
81 
83 
86  typedef ctype<_CharT> __ctype_type;
87  typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
88  __num_put_type;
89  typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
90  __num_get_type;
92 
93  // Data members:
94  protected:
95  basic_ostream<_CharT, _Traits>* _M_tie;
96  mutable char_type _M_fill;
97  mutable bool _M_fill_init;
98  basic_streambuf<_CharT, _Traits>* _M_streambuf;
99 
100  // Cached use_facet<ctype>, which is based on the current locale info.
101  const __ctype_type* _M_ctype;
102  // For ostream.
103  const __num_put_type* _M_num_put;
104  // For istream.
105  const __num_get_type* _M_num_get;
106 
107  public:
109 
115  operator void*() const
116  { return this->fail() ? 0 : const_cast<basic_ios*>(this); }
117 
118  bool
119  operator!() const
120  { return this->fail(); }
122 
130  iostate
131  rdstate() const
132  { return _M_streambuf_state; }
133 
141  void
142  clear(iostate __state = goodbit);
143 
150  void
151  setstate(iostate __state)
152  { this->clear(this->rdstate() | __state); }
153 
154  // Flip the internal state on for the proper state bits, then re
155  // throws the propagated exception if bit also set in
156  // exceptions().
157  void
158  _M_setstate(iostate __state)
159  {
160  // 27.6.1.2.1 Common requirements.
161  // Turn this on without causing an ios::failure to be thrown.
162  _M_streambuf_state |= __state;
163  if (this->exceptions() & __state)
165  }
166 
173  bool
174  good() const
175  { return this->rdstate() == 0; }
176 
183  bool
184  eof() const
185  { return (this->rdstate() & eofbit) != 0; }
186 
194  bool
195  fail() const
196  { return (this->rdstate() & (badbit | failbit)) != 0; }
197 
204  bool
205  bad() const
206  { return (this->rdstate() & badbit) != 0; }
207 
215  iostate
216  exceptions() const
217  { return _M_exception; }
218 
250  void
251  exceptions(iostate __except)
252  {
253  _M_exception = __except;
254  this->clear(_M_streambuf_state);
255  }
256 
257  // Constructor/destructor:
263  explicit
264  basic_ios(basic_streambuf<_CharT, _Traits>* __sb)
265  : ios_base(), _M_tie(0), _M_fill(), _M_fill_init(false), _M_streambuf(0),
266  _M_ctype(0), _M_num_put(0), _M_num_get(0)
267  { this->init(__sb); }
268 
275  virtual
276  ~basic_ios() { }
277 
278  // Members:
288  basic_ostream<_CharT, _Traits>*
289  tie() const
290  { return _M_tie; }
291 
300  basic_ostream<_CharT, _Traits>*
301  tie(basic_ostream<_CharT, _Traits>* __tiestr)
302  {
303  basic_ostream<_CharT, _Traits>* __old = _M_tie;
304  _M_tie = __tiestr;
305  return __old;
306  }
307 
314  basic_streambuf<_CharT, _Traits>*
315  rdbuf() const
316  { return _M_streambuf; }
317 
340  basic_streambuf<_CharT, _Traits>*
341  rdbuf(basic_streambuf<_CharT, _Traits>* __sb);
342 
354  basic_ios&
355  copyfmt(const basic_ios& __rhs);
356 
363  char_type
364  fill() const
365  {
366  if (!_M_fill_init)
367  {
368  _M_fill = this->widen(' ');
369  _M_fill_init = true;
370  }
371  return _M_fill;
372  }
373 
383  char_type
384  fill(char_type __ch)
385  {
386  char_type __old = this->fill();
387  _M_fill = __ch;
388  return __old;
389  }
390 
391  // Locales:
403  locale
404  imbue(const locale& __loc);
405 
423  char
424  narrow(char_type __c, char __dfault) const
425  { return __check_facet(_M_ctype).narrow(__c, __dfault); }
426 
442  char_type
443  widen(char __c) const
444  { return __check_facet(_M_ctype).widen(__c); }
445 
446  protected:
447  // 27.4.5.1 basic_ios constructors
454  basic_ios()
455  : ios_base(), _M_tie(0), _M_fill(char_type()), _M_fill_init(false),
456  _M_streambuf(0), _M_ctype(0), _M_num_put(0), _M_num_get(0)
457  { }
458 
465  void
466  init(basic_streambuf<_CharT, _Traits>* __sb);
467 
468  void
469  _M_cache_locale(const locale& __loc);
470  };
471 
472 _GLIBCXX_END_NAMESPACE_VERSION
473 } // namespace
#define false
Definition: stdbool.h:35
#define __throw_exception_again
Definition: exception_defines.h:37