______________________________________________________________________
27 Input/output library [lib.input.output]
______________________________________________________________________
1 This clause describes components that C++ programs may use to perform
input/output operations.
2 The following subclauses describe requirements for stream parameters,
and components for forward declarations of iostreams, predefined
iostreams objects, base iostreams classes, stream buffering, stream
formatting and manipulators, string streams, and file streams, as sum
marized in Table 1:
Table 1--Input/output library summary
+----------------------------------------------------------------+
| Subclause Header(s) |
+----------------------------------------------------------------+
|_lib.iostreams.requirements_ Requirements |
+----------------------------------------------------------------+
|_lib.iostream.forward_ Forward declarations <iosfwd> |
+----------------------------------------------------------------+
|_lib.iostream.objects_ Standard iostream objects <iostream> |
+----------------------------------------------------------------+
|_lib.iostreams.base_ Iostreams base classes <ios> |
+----------------------------------------------------------------+
|_lib.stream.buffers_ Stream buffers <streambuf> |
+----------------------------------------------------------------+
| <istream> |
|_lib.iostream.format_ Formatting and manipulators <ostream> |
| <iomanip> |
+----------------------------------------------------------------+
|_lib.string.streams_ String streams <sstream> |
| <cstdlib> |
+----------------------------------------------------------------+
| <fstream> |
|_lib.file.streams_ File streams <cstdio> |
| <cwchar> |
+----------------------------------------------------------------+
27.1 Iostreams requirements [lib.iostreams.requirements]
27.1.1 Definitions [lib.iostreams.definitions]
1 Additional definitions:
--character In this clause, the term ``character'' means any unit ele
ment which, treated sequentially, can represent text. The term does
not only mean char and wchar_t type objects, but any value which can
be represented by a type which provides the definitions specified in
(_lib.char.traits_).
--character container type Character container type is a class or a
type used to represent a character. It is used for one of the tem
plate parameters of the iostream class templates. A character con
tainer class shall have a trivial constructor and destructor and a
copy constructor and copy assignment operator that preserves its
value and semantics.
--iostream class templates The iostream class templates are templates
defined in this clause that take two template arguments: charT and
traits. The argument charT is a character container class, and the
argument traits is a structure which defines additional characteris
tics and functions of the character type represented by charT neces
sary to implement the iostream class templates.
--narrow-oriented iostream classes The narrow-oriented iostream
classes are the instantiations of the iostream class templates on
the character container class char and the default value of the
traits parameter. The traditional iostream classes are regarded as
the narrow-oriented iostream classes (_lib.narrow.stream.objects_).
--wide-oriented iostream classes The wide-oriented iostream classes
are the instantiations of the iostream class templates on the char
acter container class wchar_t and the default value of the traits
parameter. (_lib.wide.stream.objects_).
--repositional streams and arbitrary-positional streams A repositional
stream, can seek to only the position where we previously encoun
tered. On the other hand, an arbitrary-positional stream can seek
to any integral position within the length of the stream. Every
arbitrary-positional stream is repositional.
27.1.2 Type requirements [lib.iostreams.type.reqmts]
+------- BEGIN BOX 1 -------+
Change Most of types here were moved to the strings chapter as part of
the consolidation of char_traits. All the semantics wasn't moved. In
particular, the "Position TypeR equirements" seems not to have an
equivalent. And some of the types (SZ_T and POS_T) are referred to in
other places in this chapter which requires editorial attention.
1 So, I've place the moved items in editorial boxes, without changing
the text.
+------- END BOX 1 -------+
+------- BEGIN BOX 2 -------+
2 There are several types and functions needed for implementing the
iostream class templates. Some of these types and functions depend on
the definition of the character container type. The collection of
these functions describes the behavior which the implementation of the
iostream class templates expects to the character container class.
27.1.2.1 Type CHAR_T [lib.iostreams.char.t]
1 Those C++ programs that provide a character container type as the tem
plate parameter have to provide all of these functions as well as the
container class itself. The collection of these functions can be
regarded as the collection of the common definitions for the implemen
tation of the character container class.
2 No default definition/declaration is provided here.
3 Convertible to type INT_T.
+------- END BOX 2 -------+
+------- BEGIN BOX 3 -------+
27.1.2.2 Type INT_T [lib.iostreams.int.t]
1 Another character container type which can also hold an end-of-file
value. It is used as the return type of some of the iostream class
member functions. If CHAR_T is either char or wchar_t, INT_T shall be
int or wint_t, respectively.
+------- END BOX 3 -------+
+------- BEGIN BOX 4 -------+
27.1.2.3 Type OFF_T [lib.iostreams.off.t]
1 A type that can represent offsets to positional information.1) It is
used to represent:
--a signed displacement, measured in characters, from a specified
position within a sequence.
--an absolute position within a sequence.
2 The value OFF_T(-1) can be used as an error indicator.
3 The effect of passing to any function defined in this clause an OFF_T
value not obtained from a function defined in this clause (for
_________________________
1) It is usually a synonym for one if the signed basic integral types
whose representation at least as many bits as type long.
example, assigned an arbitrary integer), is undefined, except where
otherwise noted.
4 Convertible to type POS_T.2) But no validity of the resulting POS_T
value is ensured, whether or not the OFF_T value is valid.
+------- END BOX 4 -------+
+------- BEGIN BOX 5 -------+
27.1.2.4 Type POS_T [lib.iostreams.pos.t]
1 An implementation-defined type for seek operations which describes an
object that can store all the information necessary to reposition to
the position.
2 The type POS_T describes an object that can store all the information
necessary to restore an arbitrary sequence to a previous stream posi
tion and conversion state.3)
3 With a stream buffer for a repositional stream (but not an arbitrary-
positional stream), a C++ program can either obtain the current posi
tion of the stream buffer or specify a position previously obtained.
4 A class or built-in type P satisfies the requirements of a position
type, and a class or built-in type O satisfies the requirements of an
offset type if the following expressions are valid, as shown in Table
2.
+------- END BOX 5 -------+
27.1.2.5 Type SZ_T [lib.iostreams.sz.t]
A type that represents one of the signed basic integral types. It is
used to represent the number of characters transferred in an I/O oper
ation, or the size of I/O buffers.
+------- BEGIN BOX 6 -------+
27.1.2.6 Type STATE_T [lib.iostreams.state.t]
STATE_T is an implementation-defined value-oriented type. It holds
the conversion state, and is compatible with the function
locale::codecvt().
1 In the following table,
--P refers to type POS_T,
--p and q refer to an values of type POS_T,
_________________________
2) An implementation may use the same type for both OFF_T and POS_T.
3) The conversion state is used for sequences that translate between
wide-character and generalized multibyte encoding, as described in
Amendment 1 to the C Standard.
--O refers to type OFF_T,
--o refers to a value of type OFF_T, and
--i refers to a value of type int.
Table 2--Position type requirements
+-----------------------------------------------------------------------------------------+
|expression return type operational assertion/note |
| semantics pre/post-condition |
+-----------------------------------------------------------------------------------------+
|P(i) p == P(i) |
| note: a destructor is assumed. |
+-----------------------------------------------------------------------------------------+
|P p(i); |
|P p = i; post: p == P(i). |
+-----------------------------------------------------------------------------------------+
|P(o) POS_T converts from offset |
+-----------------------------------------------------------------------------------------+
|O(p) OFF_T converts to offset P(O(p)) == p |
+-----------------------------------------------------------------------------------------+
|p == q convertible to bool == is an equivalence relation |
+-----------------------------------------------------------------------------------------+
|p != q convertible to bool !(p==q) |
+-----------------------------------------------------------------------------------------+
|q = p + o POS_T + offset q-o == p |
|p += o |
+-----------------------------------------------------------------------------------------+
|q = p - o POS_T - offset q+o == p |
|p -= o |
+-----------------------------------------------------------------------------------------+
|o = p - q OFF_T distance q+o == p |
+-----------------------------------------------------------------------------------------+
2 The behavior of the stream after restoring the position with a POS_T
value modified using any other arithmetic operations is undefined.
3 The stream operations whose return type is POS_T may return
POS_T(OFF_T(-1)) as an invalid POS_T value to signal an error.
4 The conversion POS_T(OFF_T(-1)) constructs the invalid POS_T value,
which is available only for comparing to the return value of such mem
ber functions.
+------- END BOX 6 -------+
27.2 Forward declarations [lib.iostream.forward]
Header <iosfwd> synopsis
namespace std {
template<class charT> class basic_ios;
template<class charT> class basic_istream;
template<class charT> class basic_ostream;
typedef basic_ios<char> ios;
typedef basic_ios<wchar_t> wios;
typedef basic_istream<char> istream;
typedef basic_istream<wchar_t> wistream;
typedef basic_ostream<char> ostream;
typedef basic_ostream<wchar_t> wostream;
}
1 The template class basic_ios<charT,traits> serves as a base class for
the classes basic_istream<charT,traits> and
basic_ostream<charT,traits>.
2 The class ios is an instance of the template class basic_ios, special
ized by the type char.
3 The class wios is an instance of the template class basic_ios special
ized by the type wchar_t.
27.3 Standard iostream objects [lib.iostream.objects]
Header <iostream> synopsis
namespace std {
extern istream cin;
extern ostream cout;
extern ostream cerr;
extern ostream clog;
extern wistream wcin;
extern wostream wcout;
extern wostream wcerr;
extern wostream wclog;
}
1 The header <iostream> declares objects that associate objects with the
standard C streams provided for by the functions declared in <cstdio>
(_lib.c.files_).
2 Mixing operations on corresponding wide- and narrow-character streams
follows the same semantics as mixing such operations on FILEs, as
specified in Amendment 1 of the ISO C standard.
+------- BEGIN BOX 7 -------+
Issue 27-108: These objects need to be constructed and associations
established before dynamic initialization of file scope variables is
begun.
+------- END BOX 7 -------+
The objects are constructed, and the associations are established, the
first time an object of class basic_ios<charT,traits>::Init is con
structed. The objects are not destroyed during program execution.4)
27.3.1 Narrow stream objects [lib.narrow.stream.objects]
istream cin;
1 The object cin controls input from an unbuffered stream buffer associ
ated with the object stdin, declared in <cstdio>.
2 After the object cin is initialized, cin.tie() returns &cout.
ostream cout;
3 The object cout controls output to an unbuffered stream buffer associ
ated with the object stdout, declared in <cstdio> (_lib.c.files_).
ostream cerr;
4 The object cerr controls output to an unbuffered stream buffer associ
ated with the object stderr, declared in <cstdio> (_lib.c.files_).
5 After the object cerr is initialized, cerr.flags() & unitbuf is
nonzero.
ostream clog;
6 The object clog controls output to a stream buffer associated with the
object stderr, declared in <cstdio> (_lib.c.files_).
27.3.2 Wide stream objects [lib.wide.stream.objects]
wistream wcin;
_________________________
4) Constructors and destructors for static objects can access these
objects to read input from stdin or write output to stdout or stderr.
1 The object wcin controls input from an unbuffered stream buffer asso
ciated with the object stdin, declared in <cstdio>.
2 After the object wcin is initialized, wcin.tie() returns &wcout.
wostream wcout;
3 The object wcout controls output to an unbuffered stream buffer asso
ciated with the object stdout, declared in <cstdio> (_lib.c.files_).
wostream wcerr;
4 The object wcerr controls output to an unbuffered stream buffer asso
ciated with the object stderr, declared in <cstdio> (_lib.c.files_).
5 After the object wcerr is initialized, wcerr.flags() & unitbuf is
nonzero.
wostream wclog;
6 The object wclog controls output to a stream buffer associated with
the object stderr, declared in <cstdio> (_lib.c.files_).
27.4 Iostreams base classes [lib.iostreams.base]
Header <ios> synopsis
#include <exception> // for exception
namespace std {
typedef OFF_T streamoff;
typedef SZ_T streamsize;
class ios_base;
template<class charT, class traits = char_traits<charT> >
class basic_ios;
typedef basic_ios<char> ios;
typedef basic_ios<wchar_t> wios;
// _lib.std.ios.manip_, manipulators:
ios_base& boolalpha (ios_base& str);
ios_base& noboolalpha(ios_base& str);
ios_base& showbase (ios_base& str);
ios_base& noshowbase (ios_base& str);
ios_base& showpoint (ios_base& str);
ios_base& noshowpoint(ios_base& str);
ios_base& showpos (ios_base& str);
ios_base& noshowpos (ios_base& str);
ios_base& skipws (ios_base& str);
ios_base& noskipws (ios_base& str);
ios_base& uppercase (ios_base& str);
ios_base& nouppercase(ios_base& str);
// _lib.adjustfield.manip_ adjustfield:
ios_base& internal (ios_base& str);
ios_base& left (ios_base& str);
ios_base& right (ios_base& str);
// _lib.basefield.manip_ basefield:
ios_base& dec (ios_base& str);
ios_base& hex (ios_base& str);
ios_base& oct (ios_base& str);
// _lib.floatfield.manip_ floatfield:
ios_base& fixed (ios_base& str);
ios_base& scientific (ios_base& str);
}
27.4.1 Types [lib.stream.types]
typedef OFF_T wstreamoff;
1 The type wstreamoff is an implementation-defined type that satisfies
the requirements of type OFF_T (_lib.iostreams.off.t_).
typedef POS_T wstreampos;
2 The type wstreampos is an implementation-defined type that satisfies
the requirements of type POS_T (_lib.iostreams.pos.t_).
typedef SZ_T streamsize;
3 The type streamsize is a synonym for one of the signed basic integral
types. It is used to represent the number of characters transferred
in an I/O operation, or the size of I/O buffers.5)
27.4.2 Template struct ios_traits [lib.ios.traits]
+------- BEGIN BOX 8 -------+
Issue 27-207: ios_traits was previously defined here. Character
traits have now been consolidated into char_traits defined in the
_________________________
5) streamsize is used in most places where ISO C would use size_t.
Most of the uses of streamsize could use size_t, except for the
strstreambuf constructors, which require negative values. It should
probably be the signed type corresponding to size_t (which is what
Posix.2 calls ssize_t).
string header. Since declarations for them are needed to use the
typedefs they should probably be available here somehow.
#include <ios>
+------- END BOX 8 -------+
27.4.3 Class ios_base [lib.ios.base]
namespace std {
class ios_base {
public:
class failure;
typedef T1 fmtflags;
static const fmtflags boolalpha;
static const fmtflags dec;
static const fmtflags fixed;
static const fmtflags hex;
static const fmtflags internal;
static const fmtflags left;
static const fmtflags oct;
static const fmtflags right;
static const fmtflags scientific;
static const fmtflags showbase;
static const fmtflags showpoint;
static const fmtflags showpos;
static const fmtflags skipws;
static const fmtflags unitbuf;
static const fmtflags uppercase;
static const fmtflags adjustfield;
static const fmtflags basefield;
static const fmtflags floatfield;
typedef T2 iostate;
static const iostate badbit;
static const iostate eofbit;
static const iostate failbit;
static const iostate goodbit;
typedef T3 openmode;
static const openmode app;
static const openmode ate;
static const openmode binary;
static const openmode in;
static const openmode out;
static const openmode trunc;
typedef T4 seekdir;
static const seekdir beg;
static const seekdir cur;
static const seekdir end;
class Init;
// _lib.iostate.flags_ iostate flags:
iostate exceptions() const;
void exceptions(iostate except);
// _lib.fmtflags.state_ fmtflags state:
fmtflags flags() const;
fmtflags flags(fmtflags fmtfl);
fmtflags setf(fmtflags fmtfl);
fmtflags setf(fmtflags fmtfl, fmtflags mask);
void unsetf(fmtflags mask);
streamsize precision() const;
streamsize precision(streamsize prec);
streamsize width() const;
streamsize width(streamsize wide);
// _lib.ios.base.locales_ locales:
locale imbue(const locale& loc);
locale getloc() const;
// _lib.ios.base.storage_ storage:
static int xalloc();
long& iword(int index);
void*& pword(int index);
// destructor
~ios_base();
+------- BEGIN BOX 9 -------+
Change: The destructor always existed. N0842R1 added description
without calling for adding the declaration. The editor has added the
declaration.
+------- END BOX 9 -------+
// _lib.ios.base.callback_ callbacks;
enum event { erase_event, imbue_event, copyfmt_event };
typedef void (*event_callback)(event, ios_base&, int index);
void register_callback(event_call_back fn, int index);
protected:
ios_base();
private:
// static int index; exposition only
// long* iarray; exposition only
// void** parray; exposition only
};
}
+------- BEGIN BOX 10 -------+
Issue 27-208: Add the following declarations:
// _lib.iostate.flags_ iostate flags:
operator bool() const
bool operator!() const
iostate rdstate() const;
void clear(iostate state = goodbit);
void setstate(iostate state);
bool good() const;
bool eof() const;
bool fail() const;
bool bad() const;
ios_base& copyfmt(const ios_base& rhs);
Note that there will still be a version of copyfmt() specified for
basic_ios. The task of ``copying the state'' can be divided between
these two functions: ios_base::copyfmt() copies the current state of
the fmtflags, while basic_ios::copyfmt() copies the tie() state (if
that is indeed what is involved in copying the format).
+------- END BOX 10 -------+
1 ios_base defines several member types:
--a class failure derived from exception;
--a class Init;
--three bitmask types, fmtflags, iostate, and openmode;
--an enumerated type, seekdir.
2 It maintains several kinds of data:
--state information that reflects the integrity of the stream buffer;
--control information that influences how to interpret (format) input
sequences and how to generate (format) output sequences;
--additional information that is stored by the program for its private
use.
3 [Note: For the sake of exposition, the maintained data is presented
here as:
--static int index, specifies the next available unique index for the
integer or pointer arrays maintained for the private use of the pro
gram, initialized to an unspecified value;
--long* iarray, points to the first element of an arbitrary-length
long array maintained for the private use of the program;
--void** parray, points to the first element of an arbitrary-length
pointer array maintained for the private use of the program. --end
note]
27.4.3.1 Types [lib.ios.types]
27.4.3.1.1 Class ios_base::failure [lib.ios::failure]
namespace std {
class ios_base::failure : public exception {
public:
explicit failure(const string& msg);
virtual ~failure();
virtual const char* what() const;
};
}
1 The class failure defines the base class for the types of all objects
thrown as exceptions, by functions in the iostreams library, to report
errors detected during stream buffer operations.
explicit failure(const string& msg);
Effects:
Constructs an object of class failure, initializing the base class
with exception(msg).
Postcondition:
what() == msg.str()
const char* what() const;
Returns:
The message msg with which the exception was created.
27.4.3.1.2 Type ios_base::fmtflags [lib.ios::fmtflags]
typedef T1 fmtflags;
1 The type fmtflags is a bitmask type (_lib.bitmask.types_). Setting
its elements has the effects indicated in Table 3:
Table 3--fmtflags effects
+-----------------------------------------------------------+
| Element Effect(s) if set |
+-----------------------------------------------------------+
|boolalpha insert and extract bool type in alphabetic |
| format |
|dec converts integer input or generates integer |
| output in decimal base |
|fixed generate floating-point output in fixed-point |
| notation; |
|hex converts integer input or generates integer |
| output in hexadecimal base; |
|internal adds fill characters at a designated internal |
| point in certain generated output, or identi |
| cal to right if no such point is designated; |
|left adds fill characters on the right (final po |
| sitions) of certain generated output; |
|oct converts integer input or generates integer |
| output in octal base; |
|right adds fill characters on the left (initial po |
| sitions) of certain generated output; |
|scientific generates floating-point output in scientific |
| notation; |
|showbase generates a prefix indicating the numeric |
| base of generated integer output; |
|showpoint generates a decimal-point character uncondi |
| tionally in generated floating-point output; |
|showpos generates a + sign in non-negative generated |
| numeric output; |
|skipws skips leading white space before certain in |
| put operations; |
|unitbuf flushes output after each output operation; |
|uppercase replaces certain lowercase letters with their |
| uppercase equivalents in generated output. |
+-----------------------------------------------------------+
2 Type fmtflags also defines the constants indicated in Table 4:
Table 4--fmtflags constants
+--------------------------------------+
| Constant Allowable values |
+--------------------------------------+
|adjustfield left | right | internal |
|basefield dec | oct | hex |
|floatfield scientific | fixed |
+--------------------------------------+
27.4.3.1.3 Type ios_base::iostate [lib.ios::iostate]
typedef T2 iostate;
1 The type iostate is a bitmask type (_lib.bitmask.types_) that contains
the elements indicated in Table 5:
Table 5--iostate effects
+--------------------------------------------------------+
|Element Effect(s) if set |
+--------------------------------------------------------+
|badbit indicates a loss of integrity in an input or |
| output sequence (such as an irrecoverable |
| read error from a file); |
|eofbit indicates that an input operation reached the |
| end of an input sequence; |
|failbit indicates that an input operation failed to |
| read the expected characters, or that an out |
| put operation failed to generate the desired |
| characters. |
+--------------------------------------------------------+
2 Type iostate also defines the constant:
--goodbit, the value zero.
27.4.3.1.4 Type ios_base::openmode [lib.ios::openmode]
typedef T3 openmode;
1 The type openmode is a bitmask type (_lib.bitmask.types_). It con
tains the elements indicated in Table 6:
Table 6--openmode effects
+---------------------------------------------------+
|Element Effect(s) if set |
+---------------------------------------------------+
|app seek to end before each write |
|ate open and seek to end immediately after |
| opening |
|binary perform input and output in binary mode |
| (as opposed to text mode) |
|in open for input |
|out open for output |
|trunc truncate an existing stream when opening |
+---------------------------------------------------+
27.4.3.1.5 Type ios_base::seekdir [lib.ios::seekdir]
typedef T4 seekdir;
1 The type seekdir is an enumerated type (_lib.enumerated.types_) that
contains the elements indicated in Table 7:
Table 7--seekdir effects
+----------------------------------------------------------+
|Element Meaning |
+----------------------------------------------------------+
|beg request a seek (for subsequent input or output) |
| relative to the beginning of the stream |
|cur request a seek relative to the current position |
| within the sequence |
|end request a seek relative to the current end of |
| the sequence |
+----------------------------------------------------------+
27.4.3.1.6 Class ios_base::Init [lib.ios::Init]
namespace std {
class ios_base::Init {
public:
Init();
~Init();
private:
// static int init_cnt; exposition only
};
}
1 The class Init describes an object whose construction ensures the con
struction of the eight objects declared in <iostream>
(_lib.iostream.objects_) that associate file stream buffers with the
standard C streams provided for by the functions declared in <cstdio>
(_lib.c.files_).
2 For the sake of exposition, the maintained data is presented here as:
--static int init_cnt, counts the number of constructor and destructor
calls for class Init, initialized to zero.
Init();
Effects:
Constructs an object of class Init. If init_cnt is zero, the func
tion stores the value one in init_cnt, then constructs and initial
izes the objects cin, cout, cerr, clog
(_lib.narrow.stream.objects_), wcin, wcout, wcerr, and wclog
(_lib.wide.stream.objects_). In any case, the function then adds
one to the value stored in init_cnt.
~Init();
Effects:
Destroys an object of class Init. The function subtracts one from
the value stored in init_cnt and, if the resulting stored value is
one, calls cout.flush(), cerr.flush(), clog.flush(), wcout.flush(),
wcerr.flush(), wclog.flush().
27.4.3.2 ios_base fmtflags state functions [lib.fmtflags.state]
fmtflags flags() const;
Returns:
The format control information for both input and output.
fmtflags flags(fmtflags fmtfl);
Postcondition:
fmtfl == flags().
Returns:
The previous value of flags().
fmtflags setf(fmtflags fmtfl);
Effects:
Sets fmtfl in flags().
Returns:
The previous value of flags().
fmtflags setf(fmtflags fmtfl, fmtflags mask);
Effects:
Clears mask in flags(), sets fmtfl & mask in flags().
Returns:
The previous value of flags().
void unsetf(fmtflags mask);
Effects:
Clears mask in flags().
streamsize precision() const;
Returns:
The precision (number of digits after the decimal point) to generate
on certain output conversions.
streamsize precision(streamsize prec);
Postcondition:
prec == precision().
Returns:
The previous value of precision().
streamsize width() const;
Returns:
The minimum field width (number of characters) to generate on cer
tain output conversions.
streamsize width(streamsize wide);
Postcondition:
wide == width().
Returns:
The previous value of width().
27.4.3.3 ios_base locale functions [lib.ios.base.locales]
locale imbue(const locale loc);
Postcondition:
loc == getloc().
Returns:
The previous value of getloc().
locale getloc() const;
Effects:
Calls each registered callback pair (fn,index) (_lib.ios.callback_)
as (*fn)(imbue_event,*this,index) at such a time that a call to
ios_base::getloc() from within fn returns the new locale value loc.
Returns:
If no locale has been imbued, a copy of the global C++ locale,
locale(), in effect at the time of construction. Otherwise, returns
the imbued locale, to be used to perform locale-dependent input and
output operations.
27.4.3.4 ios_base storage functions [lib.ios.base.storage]
static int xalloc();
Returns:
index ++.
long& iword(int idx);
Effects:
If iarray is a null pointer, allocates an array of long of unspeci
fied size and stores a pointer to its first element in iarray. The
function then extends the array pointed at by iarray as necessary to
include the element iarray[idx]. Each newly allocated element of
the array is initialized to zero. The reference returned may become
invalid after another call to the object's iword member with a dif
ferent index, after a call to its copyfmt member, or when the object
is destroyed.
_________________________
6) An implementation is free to implement both the integer array
pointed at by iarray and the pointer array pointed at by parray as
sparse data structures, possibly with a one-element cache for each.
Returns:
iarray[idx].
void* & pword(int idx);
Effects:
If parray is a null pointer, allocates an array of pointers to void
of unspecified size and stores a pointer to its first element in
parray. The function then extends the array pointed at by parray as
necessary to include the element parray[idx]. Each newly allocated
element of the array is initialized to a null pointer. The refer
ence returned may become invalid after another call to the object's
pword member with a different index, after call to its copyfmt mem
ber, or when the object is destroyed.
Returns:
parray[idx].
Notes:
After a subsequent call to pword(int) for the same object, the ear
lier return value may no longer be valid. H4 "ios_base callbacks"
lib.ios.base.callback
void register_callback(event_callback fn, int index);
Effects:
Registers the pair (fn,index) such that during calls to imbue()
(_lib.ios.imbue_), copyfmt(), or ~ios_base() (_lib.ios.cons_), the
function fn is called with argument index. Functions registered are
called when an even occurs, in opposie order of registration. Func
tions registered whilce a callback function is active are not called
until the next event.
Notes:
Identical pairs are not merged. A function registered twice will be
called twice. Functions registered
27.4.3.5 ios_base constructors/destructors [lib.ios.base.cons]
ios_base();
Effects:
The ios_base members are have an indeterminate value after construc
tion.
~ios_base();
Effects:
Destroys an object of class ios_base. Calls each registered call
back pair (fn,index) (_lib.ios.base.callback_) as
(*fn)(imbue_envent,*this,index) at such ta time that any ios_base
member function called from within fn has well defined results.
27.4.4 Template class basic_ios [lib.ios]
namespace std {
template<class charT, class traits = char_traits<charT> >
class basic_ios : public ios_base {
public:
// Types:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
operator bool() const
bool operator!() const
iostate rdstate() const;
void clear(iostate state = goodbit);
void setstate(iostate state);
bool good() const;
bool eof() const;
bool fail() const;
bool bad() const;
// _lib.basic.ios.cons_ Constructor/destructor:
explicit basic_ios(basic_streambuf<charT,traits>* sb);
virtual ~basic_ios();
// _lib.basic.ios.members_ Members:
basic_ostream<charT,traits>* tie() const;
basic_ostream<charT,traits>* tie(basic_ostream<charT,traits>* tiestr);
basic_streambuf<charT,traits>* rdbuf() const;
basic_streambuf<charT,traits>* rdbuf(basic_streambuf<charT,traits>* sb);
basic_ios& copyfmt(const basic_ios& rhs);
char_type fill() const;
char_type fill(char_type ch);
// _lib.ios.base.locales_ locales:
locale imbue(const locale& loc);
protected:
basic_ios();
void init(basic_streambuf<charT,traits>* sb);
};
}
+------- BEGIN BOX 11 -------+
Issue 27-208: Move the following declarations to class ios_base:
// _lib.iostate.flags_ iostate flags:
operator bool() const
bool operator!() const
iostate rdstate() const;
void clear(iostate state = goodbit);
void setstate(iostate state);
bool good() const;
bool eof() const;
bool fail() const;
bool bad() const;
+------- END BOX 11 -------+
27.4.4.1 basic_ios constructors [lib.basic.ios.cons]
explicit basic_ios(basic_streambuf<charT,traits>* sb);
Effects:
Constructs an object of class basic_ios, assigning initial values to
its member objects by calling init(sb).
basic_ios();
Effects:
Constructs an object of class basic_ios (_lib.ios.base.cons_) leav
ing its member objects uninitialized. The object must be initialized
by calling its init member function. If it is destroyed before it
has been initialized the behavior is undefined.
void init(basic_streambuf<charT,traits>* sb);
Postconditions:
The postconditions of this function are indicated in Table 8:
Table 8--ios_base() effects
+--------------------------------------------------------+
| Element Value |
+--------------------------------------------------------+
|rdbuf() sb |
|tie() 0 |
|rdstate() goodbit if sb is not a null pointer, |
| otherwise badbit. |
|exceptions() goodbit |
|flags() skipws | dec |
|width() zero |
|precision() 6 |
|fill() the space character |
|getloc() a copy of the value returned by locale() |
|index ??? |
|iarray a null pointer |
|parray a null pointer |
+--------------------------------------------------------+
27.4.4.2 Member functions [lib.basic.ios.members]
basic_ostream<charT,traits>* tie() const;
Returns:
An output sequence that is tied to (synchronized with) the sequence
controlled by the stream buffer.
basic_ostream<charT,traits>* tie(basic_ostream<charT,traits>* tiestr);
Postcondition:
tiestr == tie().
Returns:
The previous value of tie().
basic_streambuf<charT,traits>* rdbuf() const;
Returns:
A pointer to the streambuf associated with the stream.
basic_streambuf<charT,traits>* rdbuf(basic_streambuf<charT,traits>* sb);
Postcondition:
sb == rdbuf().
Effects:
Calls clear().
+------- BEGIN BOX 12 -------+
Issue We need to consider whether the the the locale currently imbued
in the stream should be imbued into the streambuf.
+------- END BOX 12 -------+
Returns:
The previous value of rdbuf().
// _lib.ios.base.locales_ locales:
locale imbue(const locale& loc);
Effects:
Calls ios_base::imbue(loc) (_lib.ios.base.locales_) and if
rdbuf()!=0 then rdbuf()->pubimbue(loc) (_lib.streambuf.locales_).
Returns:
The prior value of ios_base::imbue().
char_type fill() const
Returns:
The character used to pad (fill) an output conversion to the speci
fied field width.
char_type fill(char_type fillch);
Postcondition:
&fillch == fill()
Returns:
The previous value of fill().
basic_ios& copyfmt(const basic_ios& rhs);
Effects:
Assigns to the member objects of *this the corresponding member
objects of rhs, except that:
--rdstate() and rdbuf() are left unchanged;
--exceptions() is altered last by calling exception(rhs.except).
--The contents of arrays pointed at by pword and iword are copied not
the pointers themselves.7)
1 If any newly stored pointer values in *this point at objects stored
outside the object rhs, and those objects are destroyed when rhs is
destroyed, the newly stored pointer values are altered to point at
newly constructed copies of the objects.
2 Before copying any parts of rhs, calls each registered callback pair
(fn,index) as (*fn)(ease_event,*this,index). After all parts but
exceptions() have been replaced, calls each callback pair that was
copied from rhs as (*fn)(copy_event,*this,index),.
Notes:
The second pass permits a copied pword value to be zeroed, or its
referent deep copied or reference counted or have other special
action taken.
Returns:
*this.
27.4.4.3 basic_ios iostate flags functions [lib.iostate.flags]
+------- BEGIN BOX 13 -------+
Issue 27-203: Having operator bool() is far from the optimal treat
ment, because among other problems, it defines expressions like
cout >> x to do something nonsensical, namely convert cout to type
bool and then shift it right x places. The traditional solution to
this problem is to have an operator void*(), but resolutions from the
Core WG may well allow for a better solution. Once it is known
whether the Core WG has resolved this problem, the Committee will
revisit these conversion operators.
+------- END BOX 13 -------+
_________________________
7) This suggests an infinite amount of copying, but the implementation
can keep track of the maximum element of the arrays that is non-zero.
operator bool() const
Returns:
!fail().
bool operator!() const
Returns:
fail().
iostate rdstate() const;
Returns:
The control state of the stream buffer.
void clear(iostate state = goodbit);
Postcondition:
state == rdstate().
Effects:
If (rdstate() & exceptions()) == 0, returns. Otherwise, the func
tion throws an object fail of class basic_ios::failure
(_lib.ios::failure_), constructed with implementation-defined argu
ment values.
void setstate(iostate state);
Effects:
Calls clear(rdstate() | state) (which may throw basic_ios::failure
(_lib.ios::failure_)).
bool good() const;
Returns:
rdstate() == 0
bool eof() const;
Returns:
true if eofbit is set in rdstate().
bool fail() const;
Returns:
true if failbit or badbit is set in rdstate().8)
_________________________
8) Checking badbit also for fail() is historical practice.
bool bad() const;
Returns:
true if badbit is set in rdstate().
iostate exceptions() const;
Returns:
A mask that determines what elements set in rdstate() cause excep
tions to be thrown.
void exceptions(iostate except);
Postcondition:
except == exceptions().
Effects:
Calls clear(rdstate()).
27.4.5 ios_base manipulators [lib.std.ios.manip]
27.4.5.1 fmtflags manipulators [lib.fmtflags.manip]
ios_base& boolalpha(ios_base& str);
Effects:
Calls str.setf(ios_base::boolalpha).
Returns:
str.
ios_base& noboolalpha(ios_base& str);
Effects:
Calls str.unsetf(ios_base::boolalpha).
Returns:
str.
ios_base& showbase(ios_base& str);
Effects:
Calls str.setf(ios_base::showbase).
Returns:
str.
ios_base& noshowbase(ios_base& str);
Effects:
Calls str.unsetf(ios_base::showbase).
Returns:
str.
ios_base& showpoint(ios_base& str);
Effects:
Calls str.setf(ios_base::showpoint).
Returns:
str.
ios_base& noshowpoint(ios_base& str);
Effects:
Calls str.unsetf(ios_base::showpoint).
Returns:
str.
ios_base& showpos(ios_base& str);
Effects:
Calls str.setf(ios_base::showpos).
Returns:
str.
ios_base& noshowpos(ios_base& str);
Effects:
Calls str.unsetf(ios_base::showpos).
Returns:
str.
ios_base& skipws(ios_base& str);
Effects:
Calls str.setf(ios_base::skipws).
Returns:
str.
ios_base& noskipws(ios_base& str);
Effects:
Calls str.unsetf(ios_base::skipws).
Returns:
str.
ios_base& uppercase(ios_base& str);
Effects:
Calls str.setf(ios_base::uppercase).
Returns:
str.
ios_base& nouppercase(ios_base& str);
Effects:
Calls str.unsetf(ios_base::uppercase).
Returns:
str.
ios_base& unitbuf(ios_base& str);
Effects:
Calls str.setf(ios_base::unitbuf).
Returns:
str.
ios_base& nounitbuf(ios_base& str);
Effects:
Calls str.unsetf(ios_base::unitbuf).
Returns:
str.
27.4.5.2 adjustfield manipulators [lib.adjustfield.manip]
ios_base& internal(ios_base& str);
Effects:
Calls str.setf(ios_base::internal, ios_base::adjustfield).
Returns:
str.
ios_base& left(ios_base& str);
Effects:
Calls str.setf(ios_base::left, ios_base::adjustfield).
Returns:
str.
ios_base& right(ios_base& str);
Effects:
Calls str.setf(ios_base::right, ios_base::adjustfield).
Returns:
str.
27.4.5.3 basefield manipulators [lib.basefield.manip]
ios_base& dec(ios_base& str);
Effects:
Calls str.setf(ios_base::dec, ios_base::basefield).
Returns:
str.
ios_base& hex(ios_base& str);
Effects:
Calls str.setf(ios_base::hex, ios_base::basefield).
Returns:
str.
ios_base& oct(ios_base& str);
Effects:
Calls str.setf(ios_base::oct, ios_base::basefield).
Returns:
str.
27.4.5.4 floatfield manipulators [lib.floatfield.manip]
ios_base& fixed(ios_base& str);
Effects:
Calls str.setf(ios_base::fixed, ios_base::floatfield).
Returns:
str.
ios_base& scientific(ios_base& str);
Effects:
Calls str.setf(ios_base::scientific, ios_base::floatfield).
_________________________
9) The function signature dec(ios_base&) can be called by the function
signature basic_ostream& stream::operator<<(basic_ostream& (*)(ba
sic_ostream&)) to permit expressions of the form cout << dec to change
the format flags stored in cout.
Returns:
str.
27.5 Stream buffers [lib.stream.buffers]
Header <streambuf> synopsis
#include <ios> // for char_traits
namespace std {
template<class charT, class traits = char_traits<charT> >
class basic_streambuf;
typedef basic_streambuf<char> streambuf;
typedef basic_streambuf<wchar_t> wstreambuf;
}
+------- BEGIN BOX 14 -------+
Should the #include be <ios> or <iosfwd> or <string>
+------- END BOX 14 -------+
1 The header <streambuf> defines types that control input from and out
put to character sequences.
27.5.1 Stream buffer requirements [lib.streambuf.reqts]
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 repre
sents, 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 candi
date 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 immedi
ately 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).
27.5.2 Template class [lib.streambuf]
basic_streambuf<charT,traits>
namespace std {
template<class charT, class traits = char_traits<charT> >
class basic_streambuf {
public:
// Types:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
virtual ~basic_streambuf();
// _lib.streambuf.locales_ locales:
locale pubimbue(const locale &loc);
locale getloc() const;
// _lib.streambuf.buffer_ buffer and positioning:
basic_streambuf<char_type,traits>*
pubsetbuf(char_type* s, streamsize n);
pos_type pubseekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which = ios_base::in | ios_base::out);
pos_type pubseekpos(pos_type sp,
ios_base::openmode which = ios_base::in | ios_base::out);
int pubsync();
// Get and put areas:
// _lib.streambuf.pub.get_ Get area:
streamsize in_avail();
int_type snextc();
int_type sbumpc();
int_type sgetc();
streamsize sgetn(char_type* s, streamsize n);
// _lib.streambuf.pub.pback_ Putback:
int_type sputbackc(char_type c);
int_type sungetc();
// _lib.streambuf.pub.put_ Put area:
int_type sputc(char_type c);
streamsize sputn(const char_type* s, streamsize n);
protected:
basic_streambuf();
// _lib.streambuf.get.area_ Get area:
char_type* eback() const;
char_type* gptr() const;
char_type* egptr() const;
void gbump(int n);
void setg(char_type* gbeg, char_type* gnext, char_type* gend);
// _lib.streambuf.put.area_ Put area:
char_type* pbase() const;
char_type* pptr() const;
char_type* epptr() const;
void pbump(int n);
void setp(char_type* pbeg, char_type* pend);
// _lib.streambuf.virtuals_ virtual functions:
// _lib.streambuf.virt.locales_ Locales:
virtual void imbue(const locale &loc);
// _lib.streambuf.virt.buffer_ Buffer management and positioning:
virtual basic_streambuf<char_type,traits>*
setbuf(char_type* s, streamsize n);
virtual pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which = ios_base::in | ios_base::out);
virtual pos_type seekpos(pos_type sp,
ios_base::openmode which = ios_base::in | ios_base::out);
virtual int sync();
// _lib.streambuf.virt.get_ Get area:
virtual int showmanyc();
virtual streamsize xsgetn(char_type* s, streamsize n);
virtual int_type underflow();
virtual int_type uflow();
// _lib.streambuf.virt.pback_ Putback:
virtual int_type pbackfail(int_type c = traits::eof());
// _lib.streambuf.virt.put_ Put area:
virtual streamsize xsputn(const char_type* s, streamsize n);
virtual int_type overflow (int_type c = traits::eof());
};
}
1 The class template basic_streambuf<charT,traits> serves as an abstract
base class for deriving various stream buffers whose objects each con
trol two character sequences:
--a character input sequence;
--a character output sequence.
2 The class streambuf is an instantiation of the template class
basic_streambuf specialized by the type char.
3 The class wstreambuf is an instantiation of the template class
basic_streambuf specialized by the type wchar_t.
27.5.2.1 basic_streambuf constructors [lib.streambuf.cons]
basic_streambuf();
Effects:
Constructs an object of class basic_streambuf<charT,traits> and
initializes:10)
--all its pointer member objects to null pointers,
--the getloc() member to to a copy the global locale, locale(), at the
time of construction.
Notes:
Once the getloc() member is initialized, results of calling locale
member functions, and of members of facets so obtained, can safely
be cached until the next time the member imbue is called.
27.5.2.2 basic_streambuf public member [lib.streambuf.members]
functions
27.5.2.2.1 Locales [lib.streambuf.locales]
locale pubimbue(const locale& loc);
Postcondition:
loc == getloc().
_________________________
10) The default constructor is protected for class basic_streambuf to
assure that only objects for classes derived from this class may be
constructed.
Effects:
Calls imbue(loc).
Returns:
Previous value of getloc().
locale getloc() const;
Returns:
If pubimbue() has ever been called, then the last value of loc sup
plied, otherwise the current global locale, locale(). If called
after pubimbue() has been called but before pubimbue has returned
(i.e. from within the call of imbue()) then it returns the previous
value.
27.5.2.2.2 Buffer management and [lib.streambuf.buffer]
positioning
basic_streambuf<char_type,traits>* pubsetbuf(char_type* s, streamsize n);
Returns:
setbuf(s,n).
pos_type pubseekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which = ios_base::in | ios_base::out);
Returns:
seekoff(off,way,which).
pos_type pubseekpos(pos_type sp,
ios_base::openmode which = ios_base::in | ios_base::out);
Returns:
seekpos(sp,which).
int pubsync();
Returns:
sync().
27.5.2.2.3 Get area [lib.streambuf.pub.get]
streamsize in_avail();
Returns:
If a read position is available, returns egptr() - gptr(). Other
wise returns showmanyc() (_lib.streambuf.virt.get_).
int_type snextc();
Effects:
Calls sbumpc().
Returns:
if that function returns traits::eof(), returns traits::eof(). Oth
erwise, returns sgetc().
Notes:
Uses traits::eof().
int_type sbumpc();
Returns:
If the input sequence read position is not available, returns
uflow(). Otherwise, returns traits::to_int_type(*gptr()) and incre
ments the next pointer for the input sequence.
int_type sgetc();
Returns:
If the input sequence read position is not available, returns under
flow(). Otherwise, returns traits::to_int_type(*gptr()).
streamsize sgetn(char_type* s, streamsize n);
Returns:
xsgetn(s,n).
27.5.2.2.4 Putback [lib.streambuf.pub.pback]
int_type sputbackc(char_type c);
Returns:
If the input sequence putback position is not available, or if
traits::eq(c,gptr()[-1]) returns false, then returns pback
fail(traits::to_int_type(c)). Otherwise, decrements the next
pointer for the input sequence and returns
traits::to_int_type(*gptr()).
int_type sungetc();
Returns:
If the input sequence putback position is not available, returns
pbackfail(). Otherwise, decrements the next pointer for the input
sequence and returns traits::to_int_type(*gptr()).
27.5.2.2.5 Put area [lib.streambuf.pub.put]
int_type sputc(char_type c);
Returns:
If the output sequence write position is not available, returns
overflow(traits::to_int_type(c)). Otherwise, stores c at the next
pointer for the output sequence, increments the pointer, and returns
traits::to_int_type(c).
streamsize sputn(const char_type* s, streamsize n);
Returns:
xsputn(s,n).
27.5.2.3 basic_streambuf protected [lib.streambuf.protected]
member functions
27.5.2.3.1 Get area access [lib.streambuf.get.area]
char_type* eback() const;
Returns:
The beginning pointer for the input sequence.
char_type* gptr() const;
Returns:
The next pointer for the input sequence.
char_type* egptr() const;
Returns:
The end pointer for the input sequence.
void gbump(int n);
Effects:
Advances the next pointer for the input sequence by n.
void setg(char_type* gbeg, char_type* gnext, char_type* gend);
Postconditions:
gbeg == eback(), gnext == gptr(), and gend == egptr().
27.5.2.3.2 Put area access [lib.streambuf.put.area]
char_type* pbase() const;
Returns:
The beginning pointer for the output sequence.
char_type* pptr() const;
Returns:
The next pointer for the output sequence.
char_type* epptr() const;
Returns:
The end pointer for the output sequence.
void pbump(int n);
Effects:
Advances the next pointer for the output sequence by n.
void setp(char_type* pbeg, char_type* pend);
Postconditions:
pbeg == pbase(), pbeg == pptr(), and pend == epptr().
27.5.2.4 basic_streambuf virtual [lib.streambuf.virtuals]
functions
27.5.2.4.1 Locales [lib.streambuf.virt.locales]
void imbue(const locale&)
Effects:
Change any translations based on locale.
Notes:
Allows the derived class to be informed of changes in locale at the
time they occur. 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.
Default behavior:
Does nothing.
27.5.2.4.2 Buffer management and [lib.streambuf.virt.buffer]
positioning
basic_streambuf* setbuf(char_type* s, streamsize n);
Effects:
Performs an operation that is defined separately for each class
derived from basic_streambuf in this clause
(_lib.stringbuf.virtuals_, _lib.filebuf.virtuals_).
Default behavior:
Returns this.
pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out);
Effects:
Alters the stream positions within one or more of the controlled
sequences in a way that is defined separately for each class derived
from basic_streambuf in this clause (_lib.stringbuf.virtuals_,
_lib.filebuf.virtuals_).
Default behavior:
Returns an object of class pos_type that stores an invalid stream
position (_lib.iostreams.definitions_).
pos_type seekpos(pos_type sp,
ios_base::openmode which = in | out);
Effects:
Alters the stream positions within one or more of the controlled
sequences in a way that is defined separately for each class derived
from basic_streambuf in this clause (_lib.stringbuf::seekpos_,
_lib.filebuf::seekpos_).
Default behavior:
Returns an object of class pos_type that stores an invalid stream
position.
int sync();
Effects:
Synchronizes the controlled sequences with the arrays. That is, if
pbase() is non-null the characters between pbase() and pptr() are
written to the controlled sequence.
+------- BEGIN BOX 15 -------+
Issue 27-312: This description does not say what happens to the input
sequence (if there is one.)
+------- END BOX 15 -------+
The pointers may then be reset as appropriate.
Returns:
-1 on failure. What constitutes failure is determined by each
derived class (_lib.filebuf.virtuals_).
Default behavior:
Returns zero.
+------- BEGIN BOX 16 -------+
Issue 27-312: Is it possible to synchonize the input sequence in all
cases? If not, can we liberalize this specification to accomodate
those constraints?
+------- END BOX 16 -------+
27.5.2.4.3 Get area [lib.streambuf.virt.get]
int showmanyc();11)
Returns:
an estimate of the number of characters available in the 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.12)
Default behavior:
Returns zero.
Notes:
Uses traits::eof().
streamsize xsgetn(char_type* s, streamsize n);
Effects:
Assigns up to n characters to successive elements of the array whose
first element is designated by s. The characters assigned are read
from the input sequence as if by repeated calls to sbumpc().
Assigning stops when either n characters have been assigned or a
call to sbumpc() would return traits::eof().
Returns:
The number of characters assigned.13)
Notes:
Uses traits::eof().
_________________________
11) The morphemes of showmanyc are "es-how-many-see", not "show-
manic".
12) undeflow or uflow might fail by throwing an exception prematurely.
The intention is not only that the calls will not return eof() but
that they will return ``immediately.''
13) Classes derived from basic_streambuf can provide more efficient
ways to implement xsgetn() and xsputn() by overriding these defini
tions from the base class.
int_type underflow();
Notes:
The public members of basic_streambuf call this virtual function
only if gptr() is null or gptr() >= egptr()
Returns:
traits::to_int_type(c), where c is the first character of the pend
ing sequence, without moving the input sequence position past it.
If the pending sequence is null then the function returns
traits::eof() to indicate failure.
1 The pending sequence of characters is defined as the concatenation of:
a)If gptr() is non- NULL, then the egptr() - gptr() characters start
ing at gptr(), otherwise the empty sequence.
b)Some sequence (possibly empty) of characters read from the input
sequence.
2 The result character is
a)If the pending sequence is non-empty, the first character of the
sequence.
b)If the pending sequence empty then the next character that would be
read from the input sequence.
3 The backup sequence is defined as the concatenation of:
a)If eback() is null then empty,
b)Otherwise the gptr() - eback() characters beginning at eback().
Effects:
The function sets up the gptr() and egptr() satisfying one of:
a)If the pending sequence is non-empty, egptr() is non-null and
egptr() - gptr() characters starting at gptr() are the characters in
the pending sequence
b)If the pending sequence is empty, either gptr() is null or gptr()
and egptr() are set to the same non-NULL pointer.
4 If eback() and gptr() are non-null then the function is not con
strained as to their contents, but the ``usual backup condition'' is
that either:
a)If the backup sequence contains at least gptr() - eback() charac
ters, then the gptr() - eback() characters starting at eback() agree
with the last gptr() - eback() characters of the backup sequence.
b)Or the n characters starting at gptr() - n agree with the backup
sequence (where n is the length of the backup sequence)
Default behavior:
Returns traits::eof().
int_type uflow();
Requires:
The constraints are the same as for underflow(), except that the
result character is transferred from the pending sequence to the
backup sequence, and the pending sequence may not be empty before
the transfer.
Default behavior:
Calls underflow(). If underflow() returns traits::eof(), returns
traits::eof(). Otherwise, returns the value of
traits::to_int_type(*gptr()) and increment the value of the next
pointer for the input sequence.
Returns:
traits::eof(c) to indicate failure.
27.5.2.4.4 Putback [lib.streambuf.virt.pback]
int_type pbackfail(int_type c = traits::eof());
Notes:
The public functions of basic_streambuf call this virtual function
only when gptr() is null, gptr() == eback(), or
traits::eq(*gptr(),traits::to_char_type(c)) returns false. Other
calls shall also satisfy that constraint.
The pending sequence is defined as for underflow(), with the modifi
cations that
--If traits::eq_int_type(c,traits::eof()) returns true, then the input
sequence is backed up one character before the pending sequence is
determined.
--If traits::eq_int_type(c,traits::eof()) return false, then c is
prepended. Whether the input sequence is backed up or modified in
any other way is unspecified.
Postcondition:
On return, the constraints of gptr(), eback(), and pptr() are the
same as for underflow().
Returns:
traits::eof() to indicate failure. Failure may occur because the
input sequence could not be backed up, or if for some other reason
the pointers could not be set consistent with the constraints.
pbackfail() is called only when put back has really failed.
Returns some value other than traits::eof() to indicate success.
Default behavior:
Returns traits::eof().
27.5.2.4.5 Put area [lib.streambuf.virt.put]
streamsize xsputn(const char_type* s, streamsize n);
Effects:
Writes up to n characters to the output sequence as if by repeated
calls to sputc(c). The characters written are obtained from succes
sive elements of the array whose first element is designated by s.
Writing stops when either n characters have been written or a call
to sputc(c) would return traits::eof().
Returns:
The number of characters written.
int_type overflow(int_type c = traits::eof());
Effects:
Consumes some initial subsequence of the characters of the pending
sequence. The pending sequence is defined as the concatenation of
a)if pbase() is NULL then the empty sequence otherwise, pptr() -
pbase() characters beginning at pbase().
b)if traits::eq_int_type(c,traits::eof()) returns true, then the empty
sequence otherwise, the sequence consisting of c.
Notes:
The member functions sputc() and sputn() call this function in case
that no room can be found in the put buffer enough to accomodate the
argument character sequence.
Requires:
Every overriding definition of this virtual function shall obey the
following constraints:
1)The effect of consuming a character on the associated output
sequence is specified14)
2)Let r be the number of characters in the pending sequence not con
sumed. If r is non-zero then pbase() and pptr() must be set so
that: pptr() - pbase() == r and the r characters starting at pbase()
are the associated output stream. In case r is zero (all charac
ters of the pending sequence have been consumed) then either pbase()
is set to NULL, or pbase() and pptr() are both set to the same non-
NULL value.
3)The function may fail if either appending some character to the
associated output stream fails or if it is unable to establish
_________________________
14) That is, for each class derived from an instance of ba
sic_streambuf in this clause (_lib.stringbuf_, _lib.filebuf_), a spec
ification of how consuming a character effects the associated output
sequence is given. There is no requirement on a program-defined
class.
pbase() and pptr() according to the above rules.
Returns:
traits::eof() or throws an exception if the function fails.
Otherwise, returns some value other than traits::eof() to indicate
success.15)
Default behavior:
Returns traits::eof().
27.6 Formatting and manipulators [lib.iostream.format]
Header <istream> synopsis
#include <ios> // for char_traits
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_istream;
typedef basic_istream<char> istream;
typedef basic_istream<wchar_t> wistream;
template<class charT, class traits>
basic_istream<charT,traits>& ws(basic_istream<charT,traits>& is);
}
Header <ostream> synopsis
#include <ios> // for char_traits
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_ostream;
typedef basic_ostream<char> ostream;
typedef basic_ostream<wchar_t> wostream;
template<class charT, class traits>
basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
template<class charT, class traits>
basic_ostream<charT,traits>& ends(basic_ostream<charT,traits>& os);
template<class charT, class traits>
basic_ostream<charT,traits>& flush(basic_ostream<charT,traits>& os);
}
Header <iomanip> synopsis
_________________________
15) Typically, overflow returns c to indicate success, except when
traits::eq_int_type(c,traits::eof()) returns true, in which case it
returns traits::not_eof(c).
#include <ios>
namespace std {
// Types T1, T2, ... are unspecified
// implementation types
T1 resetiosflags(ios_base::fmtflags mask);
T2 setiosflags (ios_base::fmtflags mask);
T3 setbase(int base);
T4 setfill(int c);
T5 setprecision(int n);
T6 setw(int n);
}
27.6.1 Input streams [lib.input.streams]
1 The header <istream> defines two types and a function signature that
control input from a stream buffer.
27.6.1.1 Template class basic_istream [lib.istream]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_istream : virtual public basic_ios<charT,traits> {
public:
// Types:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
// _lib.istream.cons_ Constructor/destructor:
explicit basic_istream(basic_streambuf<charT,traits>* sb);
virtual ~basic_istream();
// _lib.istream::sentry_ Prefix/suffix:
class sentry;
// _lib.istream.formatted_ Formatted input:
basic_istream<charT,traits>& operator>>
(basic_istream<charT,traits>& (*pf)(basic_istream<charT,traits>&))
basic_istream<charT,traits>& operator>>
(basic_ios<charT,traits>& (*pf)(basic_ios<charT,traits>&))
basic_istream<charT,traits>& operator>>(char_type* s);
basic_istream<charT,traits>& operator>>(char_type& c);
basic_istream<charT,traits>& operator>>(bool& n);
basic_istream<charT,traits>& operator>>(short& n);
basic_istream<charT,traits>& operator>>(unsigned short& n);
basic_istream<charT,traits>& operator>>(int& n);
basic_istream<charT,traits>& operator>>(unsigned int& n);
basic_istream<charT,traits>& operator>>(long& n);
basic_istream<charT,traits>& operator>>(unsigned long& n);
basic_istream<charT,traits>& operator>>(float& f);
basic_istream<charT,traits>& operator>>(double& f);
basic_istream<charT,traits>& operator>>(long double& f);
basic_istream<charT,traits>& operator>>(void*& p);
basic_istream<charT,traits>& operator>>
(basic_streambuf<char_type,traits>* sb);
// _lib.istream.unformatted_ Unformatted input:
streamsize gcount() const;
int_type get();
basic_istream<charT,traits>& get(char_type& c);
basic_istream<charT,traits>& get(char_type* s, streamsize n,
char_type delim = traits::newline());
basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb,
char_type delim = traits::newline());
basic_istream<charT,traits>& getline(char_type* s, streamsize n,
char_type delim = traits::newline());
basic_istream<charT,traits>& ignore
(streamsize n = 1, int_type delim = traits::eof());
int_type peek();
basic_istream<charT,traits>& read (char_type* s, streamsize n);
streamsize readsome(char_type* s, streamsize n);
basic_istream<charT,traits>& putback(char_type c);
basic_istream<charT,traits>& unget();
int sync();
pos_type tellg();
basic_istream<charT,traits>& seekg(pos_type);
basic_istream<charT,traits>& seekg(off_type, ios_base::seekdir);
};
}
1 The class basic_istream defines a number of member function signatures
that assist in reading and interpreting input from sequences con
trolled by a stream buffer.
2 Two groups of member function signatures share common properties: the
formatted input functions (or extractors) and the unformatted input
functions. Both groups of input functions are described as if they
obtain (or extract) input characters by calling rdbuf()->sbumpc() or
rdbuf()->sgetc(). They may use other public members of istream.
+------- BEGIN BOX 17 -------+
Issue 27-413: X3J16/95-0149==WG21/N0749 deleted a sentence that pre
vented the functions from calling other streambuf virtuals (such as
seek.)
+------- END BOX 17 -------+
3 If rdbuf()->sbumpc() or rdbuf()->sgetc() returns traits::eof(), then
the input function, except as explicitly noted otherwise, completes
its actions and does setstate(eofbit), which may throw
ios_base::failure (_lib.iostate.flags_), before returning.
+------- BEGIN BOX 18 -------+
Issue: The timing here ought to be that eofbit is set at the time a
function returns traits::eof() but throwing of an exception (because
eofbit is on in exceptions() is deferred until the function would oth
erwise return. The conseequence would be that under various condi
tions the function might set more than one bit in the error state.
+------- END BOX 18 -------+
4 If one of these called functions throws an exception, then unless
explicitly noted otherwise the input function set badbit in error
state. If badbit is on in exception(), the input function rethrows
the exception without completing its actions, otherwise it does not
throw anything and treat as an error.
27.6.1.1.1 basic_istream constructors [lib.istream.cons]
explicit basic_istream(basic_streambuf<charT,traits>* sb);
Effects:
Constructs an object of class basic_istream, assigning initial val
ues to the base class by calling basic_ios::init(sb)
(_lib.basic.ios.cons_).
Postcondition:
gcount() == 0
virtual ~basic_istream();
Effects:
Destroys an object of class basic_istream.
Notes:
Does not perform any operations of rdbuf().
27.6.1.1.2 Class basic_istream::sentry [lib.istream::sentry]
namespace std {
template <class charT,class traits = char_traits<charT> >
class basic_istream<charT,traits>::sentry {
bool ok_; // exposition only
public:
explicit sentry(basic_istream<charT,traits>& is, bool noskipws = false);
~sentry();
operator bool() { return ok_; }
};
}
1 The class sentry defines a class that is responsible for doing excep
tion safe prefix and suffix operations.
explicit sentry(basic_istream<charT,traits>& is, bool noskipws = false);
Effects:
If is.good() is true, prepares for formatted or unformatted input.
First, if is.tie() is not a null pointer, the function calls
is.tie()->flush() to synchronize the output sequence with any asso
ciated external C stream.16) If noskipws is zero and is.flags() &
_________________________
ios_base::skipws is nonzero, the function extracts and discards each
character as long as the next available input character c is a
whitespace character.
Notes:
The constructor explicit sentry(basic_istream<charT,traits>& is,
bool noskipws = false) uses the currently imbued locale in is , to
determine whether the next input character is whitespace or not.
2 To decide if the character c is a whitespace character, the construc
tor performs ``as if'' it executes the following code fragment:
const ctype<charT>& ctype = use_facet<ctype<charT> >(is.getloc());
if (ctype.is(ctype.space,c)!=0)
// c is a whitespace character.
3 If, after any preparation is completed, is.good() is true, ok_ == true
otherwise, ok_ == false. During preparation, the constructor may call
setstate(failbit) (which may throw ios_base::failure
(_lib.iostate.flags_))17)
4 [Example: A typical implementation of the sentry constructor might
include code such as:
template <class charT, class traits = char_traits<charT> >
basic_istream<charT,traits>::sentry(
basic_istream<charT,traits>& is, bool noskipws = false) {
...
int_type c;
typedef ctype<charT> ctype_type;
const ctype_type& ctype = use_facet<ctype_type>(is.getloc());
while ((c = is.rdbuf()->snextc()) != traits::eof()) {
if (ctype.is(ctype.space,c)==0) {
is.rdbuf()->sputbackc (c);
break;
}
}
...
}
--end example]
~sentry();
Effects:
None.
operator bool();
_________________________
16) The call is.tie()->flush() does not necessarily occur if the func
tion can determine that no synchronization is necessary.
17) The sentry constructor and destructor can also perform additional
implementation-dependent operations.
Effects:
Returns ok_.
27.6.1.2 Formatted input functions [lib.istream.formatted]
27.6.1.2.1 Common requirements [lib.istream.formatted.reqmts]
1 Each formatted input function begins execution by constructing an
object of class sentry with the default argument noskipws = false. If
this object returns true, while converting to a value of type bool,
the function endeavors to obtain the requested input. If an exception
is thrown during input then ios::badbit is turned on18) in *this's
error state. If (exception()&badbit)!= 0 then the exception is
rethrown In any case, the formatted input function ends by destructing
the sentry object , then returns *this
+------- BEGIN BOX 19 -------+
Change: Description of exception handling was added as authorized by
Santa Cruz motion for aggressive editing with regards to exception
handling.
+------- END BOX 19 -------+
27.6.1.2.2 Arithmetic [lib.istream.formatted.arithmetic]
Extractors
+------- BEGIN BOX 20 -------+
Change: The definitions of the arithmetic extractors have been consol
idated here as part of the aggressive editing authorized in Monterey.
+------- END BOX 20 -------+
operator>>(short& val);
operator>>(unsigned short& val);
operator>>(int& val);
operator>>(unsigned int& val);
operator>>(long& val);
operator>>(unsigned long& val);
operator>>(float& val);
operator>>(double& val);
operator>>(long double& val);
operator>>(bool& val);
As in the case of the inserters, these extractors depend on the
locale's num_get<> (_lib.locale.num.get_) object to perform parsing
the input stream data. The conversion occurs as if it performed the
following code fragment:
_________________________
18) This is done without causing an ios::failure to be thrown.
typedef num_get< charT,istreambuf_iterator<charT,traits> > numget;
iostate err = 0;
use_facet< numget >(loc).get(*this, 0, *this, err, val);
setstate(err);
+------- BEGIN BOX 21 -------+
Change: As part of the aggressive editing the use of a temporary has
been eliminated.
+------- END BOX 21 -------+
In the above fragment, loc stands for the private member of the
basic_ios class,
1 The first argument provides an object of the istream_iterator class
which is an iterator pointed to an input stream. It bypasses istreams
and uses streambufs directly. Class locale relies on this type as its
interface to istream, since the flexibility it has been abstracted
away from direct dependence on istream.
27.6.1.2.3 basic_istream::operator>> [lib.istream::extractors]
basic_istream<charT,traits>& operator>>
(basic_istream<charT,traits>& (*pf)(basic_istream<charT,traits>&))
Returns:
pf(*this).19)
basic_istream<charT,traits>& operator>>
(basic_ios<charT,traits>& (*pf)(basic_ios<charT,traits>&));
Effects:
Calls pf(*this), then returns *this.20)
+------- BEGIN BOX 22 -------+
Issue 27-901: The status of char_type operators needs to be clarified.
+------- END BOX 22 -------+
basic_istream<charT,traits>& operator>>(char_type* s);
Effects:
Extracts characters and stores them into successive locations of an
array whose first element is designated by s.21) If width() is
_________________________
19) See, for example, the function signature ws(basic_istream&)
(_lib.istream.manip_).
20) See, for example, the function signature dec(ios_base&)
(_lib.basefield.manip_).
21) Note that this function is not overloaded on types signed char and
unsigned char.
greater than zero, n is width(). Otherwise n is the the number of
elements of the largest array of char_type that can store a termi
nating eos. n is the maximum number of characters stored.
1 Characters are extracted and stored until any of the following occurs:
--n-1 characters are stored;
--end-of-file occurs on the input sequence;
--ctype.is(ctype.space,c) is true for the next available input charac
ter c. In the above code fragment, the argument ctype is acquired
by use_facet<ctype<charT> >(getloc()).
2 A null byte ( charT(0)) in the next position, which may be the first
position if no characters were extractes. And call width(0).
3 If the function extracted no characters, it calls setstate(failbit),
which may throw ios_base::failure (_lib.iostate.flags_).
Returns:
*this.
Notes:
Uses traits::eos().
basic_istream<charT,traits>& operator>>(char_type& c);
Effects:
Extracts a character, if one is available, and stores it in c. Oth
erwise, the function calls setstate(failbit).
Returns:
*this.
basic_istream<charT,traits>& operator>>(void*& p);
Effects:
Converts a pointer to void, if one is available, and stores it in p.
Returns:
*this.
+------- BEGIN BOX 23 -------+
Issue 27-607 The above description needs work. Maybe there should be
a getnum for it. (Using %p conversion specifier).
+------- END BOX 23 -------+
basic_istream<charT,traits>& operator>>
(basic_streambuf<charT,traits>* sb);
Requires:
sb shall be non-null.
Effects:
If sb is null, calls setstate(badbit), which may throw
ios_base::failure (_lib.iostate.flags_).
Extracts characters from *this and inserts them in the output
sequence controlled by sb. Characters are extracted and inserted
until any of the following occurs:
--end-of-file occurs on the input sequence;
--inserting in the output sequence fails (in which case the character
to be inserted is not extracted);
--an exception occurs (in which case the exception is caught).
4 If the function inserts no characters, it calls setstate(failbit),
which may throw ios_base::failure (_lib.iostate.flags_). If failure
was due to catching an exception thrown while extracting characters
from sb and failbit is on in exceptions() (_lib.iostate.flags_), then
the caught exception is rethrown.
Returns:
*this.
27.6.1.3 Unformatted input functions [lib.istream.unformatted]
1 Each unformatted input function begins execution by constructing an
object of class sentry with the default argument noskipws = true. If
this object returns true, while converting to a value of type bool,
the function endeavors to obtain the requested input. It also counts
the number of characters extracted. In any case, the unformatted
input function ends by storing the count in a member object and
destructing the sentry object , then returning the value specified for
the unformatted input function.
streamsize gcount() const;
Returns:
The number of characters extracted by the last unformatted input
member function called for the object.
int_type get();
Effects:
Extracts a character c, if one is available. Otherwise, the func
tion calls setstate(failbit), which may throw ios_base::failure
(_lib.iostate.flags_),
Returns:
c if available, otherwise traits::eof().
basic_istream<charT,traits>& get(char_type& c);
Effects:
Extracts a character, if one is available, and assigns it to c.22)
Otherwise, the function calls setstate(failbit) (which may throw
ios_base::failure (_lib.iostate.flags_)).
Returns:
*this.
basic_istream<charT,traits>& get(char_type* s, streamsize n,
char_type delim = traits::newline());
Effects:
Extracts characters and stores them into successive locations of an
array whose first element is designated by s.23) Characters are
extracted and stored until any of the following occurs:
--n - 1 characters are stored;
--end-of-file occurs on the input sequence (in which case the function
calls setstate(eofbit));
--c == delim for the next available input character c (in which case c
is not extracted).
2 If the function stores no characters, it calls setstate(failbit)
(which may throw ios_base::failure (_lib.iostate.flags_)). In any
case, it then stores a null character into the next successive loca
tion of the array.
Returns:
*this.
basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb,
char_type delim = traits::newline());
Effects:
Extracts characters and inserts them in the output sequence con
trolled by sb. Characters are extracted and inserted until any of
the following occurs:
--end-of-file occurs on the input sequence;
--inserting in the output sequence fails (in which case the character
to be inserted is not extracted);
--c == delim for the next available input character c (in which case c
is not extracted);
_________________________
22) Note that this function is not overloaded on types signed charand
unsigned char.
23) Note that this function is not overloaded on types signed char and
unsigned char.
--an exception occurs (in which case, the exception is caught but not
rethrown).
3 If the function inserts no characters, it calls setstate(failbit),
which may throw ios_base::failure (_lib.iostate.flags_).
Returns:
*this.
basic_istream<charT,traits>& getline(char_type* s, streamsize n,
char_type delim = traits::newline());
Effects:
Extracts characters and stores them into successive locations of an
array whose first element is designated by s.24) Characters are
extracted and stored until one of the following occurs:
1)end-of-file occurs on the input sequence (in which case the function
calls setstate(eofbit));
2)c == delim for the next available input character c (in which case
the input character is extracted but not stored);25)
3)n - 1 characters are stored (in which case the function calls set
state(failbit)).
4 These conditions are tested in the order shown.26)
5 If the function extracts no characters, it calls setstate(failbit)
(which may throw ios_base::failure (_lib.iostate.flags_)).27)
6 In any case, it then stores a null character (using traits::eos())
into the next successive location of the array.
Returns:
*this.
7 [Example:
_________________________
24) Note that this function is not overloaded on types signed char and
unsigned char.
25) Since the final input character is ``extracted,'' it is counted in
the gcount(), even though it is not stored.
26) This allows an input line which exactly fills the buffer, without
setting failbit. This is different behavior than the historical AT&T
implementation.
27) This implies an empty input line will not cause failbit to be set.
#include <iostream>
int main()
{
using namespace std;
const int line_buffer_size = 100;
char buffer[line_buffer_size];
int line_number = 0;
while (cin.getline(buffer, line_buffer_size) || cin.gcount()) {
int count = cin.gcount();
if (cin.eof())
cout << "Partial final line"; // cin.fail() is false
else if (cin.fail()) {
cout << "Partial long line";
cin.clear(cin.rdstate() & ~ios::failbit);
} else {
count--; // Don't include '\n' in count
cout << "Line " << ++line_number;
}
cout << " (" << count << " chars): " << buffer << endl;
}
}
--end example]
basic_istream<charT,traits>&
ignore(int n = 1, int_type delim = traits::eof());
Effects:
Extracts characters and discards them. Characters are extracted
until any of the following occurs:
--if n != numeric_limits<int>::max() (_lib.limits_), n characters are
extracted
--end-of-file occurs on the input sequence (in which case the function
calls setstate(eofbit), which may throw ios_base::failure
(_lib.iostate.flags_));
--c == delim for the next available input character c (in which case c
is extracted).
Notes:
The last condition will never occur if delim == traits::eof().
Returns:
*this.
int_type peek();
Returns:
traits::eof() if good() is false. Otherwise, returns
rdbuf()->sgetc().
basic_istream<charT,traits>& read(char_type* s, streamsize n);
Effects:
Extracts characters and stores them into successive locations of an
array whose first element is designated by s.28) Characters are
extracted and stored until either of the following occurs:
--n characters are stored;
--end-of-file occurs on the input sequence (in which case the function
calls setstate(failbit), which may throw ios_base::failure
(_lib.iostate.flags_)).
Returns:
*this.
streamsize readsome(char_type* s, streamsize n);
Effects:
+------- BEGIN BOX 24 -------+
Issue 27-414: If !good() calls setstate(failbit) and returns.
+------- END BOX 24 -------+
Otherwise extracts characters and stores them into successive loca
tions of an array whose first element is designated by s. If
rdbuf()->in_avail() == -1, calls setstate(eofbit) (which may throw
ios_base::failure (_lib.iostate.flags_)), and extracts no characters;
--If rdbuf()->in_avail() == 0, extracts no characters
--If rdbuf()->in_avail() > 0, extracts min(rdbuf()->in_avail(),n)).
Returns:
The number of characters extracted.
basic_istream<charT,traits>& putback(char_type c);
Effects:
If rdbuf() is not null, calls rdbuf->sputbackc() . If rdbuf() is
null, or if sputback() returns traits::eof(), calls setstate(badbit)
(which may throw ios_base::failure (_lib.iostate.flags_)).
+------- BEGIN BOX 25 -------+
Issue 27-414: Should not call virtual if good()r eturns false.
+------- END BOX 25 -------+
_________________________
28) Note that this function is not overloaded on types signed char and
unsigned char.
Returns:
*this.
basic_istream<charT,traits>& unget();
Effects:
If rdbuf() is not null, calls rdbuf()->sungetc(). If rdbuf() is
null, or if sungetc() returns traits::eof(), calls setstate(badbit)
(which may throw ios_base::failure (_lib.iostate.flags_)).
+------- BEGIN BOX 26 -------+
EditorialProposal Issue 27-414: Should not call virtual if good()
returns false.
+------- END BOX 26 -------+
Returns:
*this.
int sync();
Effects:
If rdbuf() is a null pointer, returns -1 . Otherwise, calls
rdbuf()->pubsync() and, if that function returns -1 calls set
state(badbit) (which may throw ios_base::failure
(_lib.iostate.flags_), and returns traits::eof(). Otherwise,
returns zero.
pos_type tellg();
Returns:
if fail() == true, returns pos_type(-1) to indicate failure. Other
wise, returns rdbuf()->pubseekoff(0, cur, in).
+------- BEGIN BOX 27 -------+
Change: pos_type(-1) in above sentence replaces streampos(-1)
+------- END BOX 27 -------+
basic_istream<charT,traits>& seekg(pos_type pos);
Effects:
If fail() != true, executes rdbuf()->pubseekpos(pos).
Returns:
*this.
basic_istream<charT,traits>& seekg(off_type& off, ios_base::seekdir dir);
Effects:
If fail() != true, executes rdbuf()->pubseekoff(off, dir).
Returns:
*this.
27.6.1.4 Standard basic_istream manipulators [lib.istream.manip]
namespace std {
template<class charT, class traits>
basic_istream<charT,traits>& ws(basic_istream<charT,traits>& is);
}
Effects:
Skips any whitespace in the input sequence: saves a copy of the fmt
flags, by storing the result of the call to is.flags(), calls
is.setf(ios_base::skipws), then constructs a sentry object and
restores the fmtflags to their saved values.
Returns:
is. H4 "Template class basic_iostream" lib.iostreamclass
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_iostream :
public basic_istream<charT,traits>,
public basic_ostream<charT,traits> {
public:
// constructor/destructor
explict basic_iostream(basic_streambuf<charT,traits>* sb);
virtual ~basic_iostream();
};
}
1 The class basic_iostream inherits a number of functions that allow
reading input and writing output to sequences controlled by a stream
buffer.
27.6.1.4.1 basic_iostream constructors [lib.iostream.cons]
explicit basic_iostream(basic_streambuf<charT,traits>* sb);
Effects
Constructs an object of class basic_iostream, assigning initial val
ues to the base classes by calling basic_istream<charT,traits>(sb)
(_lib.istream_) and basic_ostream<charT,traits>(sb) (_lib.ostream_)
Postcondition:
rdbuf()==sb and gcount()==0.
virtual ~basic_iostream();
Effects:
Destroys an object of class basic_iostream.
Notes:
Does not perform any operations on rdbuf().
27.6.2 Output streams [lib.output.streams]
1 The header <ostream> defines a type and several function signatures
that control output to a stream buffer.
27.6.2.1 Template class basic_ostream [lib.ostream]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_ostream : virtual public basic_ios<charT,traits> {
public:
// Types:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
// _lib.ostream.cons_ Constructor/destructor:
explicit basic_ostream(basic_streambuf<char_type,traits>* sb);
virtual ~basic_ostream();
// _lib.ostream::sentry_ Prefix/suffix:
class sentry;
// _lib.ostream.formatted_ Formatted output:
basic_ostream<charT,traits>& operator<<
(basic_ostream<charT,traits>& (*pf)(basic_ostream<charT,traits>&));
basic_ostream<charT,traits>& operator<<
(basic_ios<charT,traits>& (*pf)(basic_ios<charT,traits>&));
basic_ostream<charT,traits>& operator<<(const char_type* s);
basic_ostream<charT,traits>& operator<<(char_type c);
basic_ostream<charT,traits>& operator<<(bool n);
basic_ostream<charT,traits>& operator<<(short n);
basic_ostream<charT,traits>& operator<<(unsigned short n);
basic_ostream<charT,traits>& operator<<(int n);
basic_ostream<charT,traits>& operator<<(unsigned int n);
basic_ostream<charT,traits>& operator<<(long n);
basic_ostream<charT,traits>& operator<<(unsigned long n);
basic_ostream<charT,traits>& operator<<(float f);
basic_ostream<charT,traits>& operator<<(double f);
basic_ostream<charT,traits>& operator<<(long double f);
basic_ostream<charT,traits>& operator<<(void* p);
basic_ostream<charT,traits>& operator<<
(basic_streambuf<char_type,traits>* sb);
// _lib.ostream.unformatted_ Unformatted output:
basic_ostream<charT,traits>& put(char_type c);
basic_ostream<charT,traits>& write(const char_type* s, streamsize n);
basic_ostream<charT,traits>& flush();
pos_type tellp();
basic_ostream<charT,traits>& seekp(pos_type);
basic_ostream<charT,traits>& seekp(off_type, ios_base::seekdir);
};
}
1 The class basic_ostream defines a number of member function signatures
that assist in formatting and writing output to output sequences con
trolled by a stream buffer.
2 Two groups of member function signatures share common properties: the
formatted output functions (or inserters) and the unformatted output
functions. Both groups of output functions generate (or insert) out
put characters by actions equivalent to calling
rdbuf().sputc(int_type). They may use other public members of
basic_ostream except that they do not invoke any virtual members of
rdbuf() except overflow().
3 If one of these called functions throws an exception, then unless
explicitly noted otherwise the output function set badbit in error
state. If badbit is on in exception(), the output function rethrows
the exception without completing its actions, otherwise it does not
throw anything and treat as an error.
27.6.2.2 basic_ostream constructors [lib.ostream.cons]
explicit basic_ostream(basic_streambuf<charT,traits>* sb);
Effects:
Constructs an object of class basic_ostream, assigning initial val
ues to the base class by calling basic_ios<charT,traits>::init(sb)
(_lib.basic.ios.cons_).
Postcondition:
rdbuf() == sb.
virtual ~basic_ostream();
Effects:
Destroys an object of class basic_ostream.
Notes:
Does not perform any operations on rdbuf().
27.6.2.3 Class basic_ostream::sentry [lib.ostream::sentry]
namespace std {
template <class charT,class traits = char_traits<charT> >
class basic_ostream<charT,traits>::sentry {
bool ok_; // exposition only
public:
explicit sentry(basic_ostream<charT,traits>& os);
~sentry();
operator bool() { return ok_; }
};
}
1 The class sentry defines a class that is responsible for doing excep
tion safe prefix and suffix operations.
explicit sentry(basic_ostream<charT,traits>& os);
2 If os.good() is nonzero, prepares for formatted or unformatted output.
If os.tie() is not a null pointer, calls os.tie()->flush().29)
3 If, after any preparation is completed, os.good() is true, ok_ == true
otherwise, ok_ == false. During preparation, the constructor may call
setstate(failbit) (which may throw ios_base::failure
(_lib.iostate.flags_))30)
~sentry();
4 If ((os.flags() & ios_base::unitbuf) && !uncaught_exception()) is
true, calls os.flush().
operator bool();
Effects:
Returns ok_.
pos_type tellp();
Returns:
if fail() == true, returns pos_type(-1) to indicate failure. Other
wise, returns rdbuf()->pubseekoff(0, cur, out).
+------- BEGIN BOX 28 -------+
Change: pos_type(-1) in above sentence replaces streampos(-1).
+------- END BOX 28 -------+
basic_ostream<charT,traits>& seekp(pos_type& pos);
Effects:
If fail() != true, executes rdbuf()->pubseekpos(pos).
Returns:
*this.
_________________________
29) The call os.tie()->flush() does not necessarily occur if the func
tion can determine that no synchronization is necessary.
30) The sentry constructor and destructor can also perform additional
implementation-dependent operations.
basic_ostream<charT,traits>& seekp(off_type& off, ios_base::seekdir dir);
If fail() != true, executes rdbuf()->pubseekoff(off, dir).
Returns:
*this.
27.6.2.4 Formatted output functions [lib.ostream.formatted]
27.6.2.4.1 Common requirements [lib.ostream.formatted.reqmts]
+------- BEGIN BOX 29 -------+
Change: This section has been subject to "aggressive editorial revi
sion" as authorized at the Santa Cruz meeting. Detailed descriptions
of the behavior of the conversions on arithmetic types has been moved
to _lib.factet.num.put.virtuals_. The descriptions of other insertors
have been revised to remove references to printf conversion speci
fiers.
+------- END BOX 29 -------+
1 Each formatted output function begins execution by constructing an
object of class sentry. If this object returns true when converted to
a value of type bool, the function endeavors to generate the requested
output. If the generation fails, then the formatted output function
does setstate(ios::fail) which might throw and exception. If an
exception is thrown during output then ios::badbit is turned on31) in
*this's error state If (exception()&badbit)!= 0 then the exception is
rethrown. Whether or not an exception is thrown the sentry object is
destroyed before leaving the formatted output function. If no excep
tion is thrown the result of the formattted output function is *this.
+------- BEGIN BOX 30 -------+
Change: The discussion of failure and exception handling has been
added, This is part of the editorial aggressive treatment of excep
tions authorized at Santa Cruz.
+------- END BOX 30 -------+
2 The descriptions of the individual formatted output operations
describes how they perform output and does not mention the sentry
object.
27.6.2.4.2 Arithmetic [lib.ostream.insertors.arithmetic]
Insertors
_________________________
31) withhout causing an ios::failure to be thrown.
operator<<(short val);
operator<<(unsigned short val);
operator<<(int val);
operator<<(unsigned int val);
operator<<(long val);
operator<<(unsigned long val);
operator<<(float val);
operator<<(double val);
operator<<(long double val);
Effects:
1 The classes num_get<> and num_put<> handle locale-dependent numeric
formatting and parsing. These inserter functions refers the imbued
locale value to utilize these numeric formatting functionality. The
formatting conversion occurs as if it performed the following code
fragment:
bool failed =
use_facet< num_put<charT,ostreambuf_iterator<charT,traits> > >(getloc()).
put(*this, *this, fill(), val). failed();
The first argument provides an object of the ostreambuf_iterator<>
class which is an iterator for class basic_ostream<>. It bypasses
ostreams and uses streambufs directly. Class locale relies on these
types as its interface to iostreams, since for flexibility it has been
abstracted away from direct dependence on ostream. The second parame
ter is a reference to the base subobject of type ios_base. It pro
vides formatting specifications such as field width, and a locale from
which to obtain other facets.
27.6.2.4.3 basic_ostream::operator<< [lib.ostream.inserters]
basic_ostream<charT,traits>& operator<<
(basic_ostream<charT,traits>& (*pf)(basic_ostream<charT,traits>&))
Returns:
pf(*this).32)
basic_ostream<charT,traits>& operator<<
(basic_ios<charT,traits>& (*pf)(basic_ios<charT,traits>&))
Effects:
Calls pf(*this).
Returns:
*this.33)
_________________________
32) See, for example, the function signature endl(basic_ostream&)
(_lib.ostream.manip_) .
33) See, for example, the function signature dec(ios_base&)
(_lib.basefield.manip_).
+------- BEGIN BOX 31 -------+
Issue 27-901: The way insertors for char_type work needs to be clari
fied. The current discussion seems to assume that char_type is char.
+------- END BOX 31 -------+
basic_ostream<charT,traits>& operator<<(const char_type* s);
Requires:
s shall be a null-terminated byte string.
Effects:
Converts the NTBS s with the conversion specifier s.
Returns:
*this.
basic_ostream<charT,traits>& operator<<(char_type c);
Effects:
Converts the char_type c with the conversion specifier c and a field
width of zero.34)
Notes:
The stored field width ( basic_ios<charT,traits>::width()) is not
set to zero.
Returns:
*this.
basic_ostream<charT,traits>& operator<<(bool n);
1 Behaves as if:
use_facet<num_put<charT,istreambuf_iterator<charT,traits> >(getloc())
.put(*this, *this, fill(), n);
which writes out a 0 or 1, or the results of use_facet<numpunct<charT>
>(getloc()).truename() or falsename() (_lib.category.numeric_),
according as whether the boolalpha flag is set.
Returns:
*this.
basic_ostream<charT,traits>& operator<<(void* p);
Effects:
Converts the pointer to void p with the conversion specifier p.
Returns:
*this.
_________________________
34) Note that this function is not overloaded on types signed char and
unsigned char.
+------- BEGIN BOX 32 -------+
Issue 27-607 This needs attention, there is nothing in the locale to
deal with %p. As a consequence of the aggressive editting the void*
insertor remains as the only reference to conversion specifiers in
this section.
+------- END BOX 32 -------+
basic_ostream<charT,traits>& operator<<
(basic_streambuf<charT,traits>* sb);
Effects:
If sb is null calls setstate(badbit) (which may throw
ios_base::failure).
2 Gets characters from sb and inserts them in *this. Characters are
read from sb and inserted until any of the following occurs:
--end-of-file occurs on the input sequence;
--inserting in the output sequence fails (in which case the character
to be inserted is not extracted);
--an exception occurs while getting a character from sb.
3 If the function inserts no characters, it calls setstate(failbit)
(which may throw ios_base::failure (_lib.iostate.flags_)). If an
exception was thrown while extracting a character, the function set
failbit in error state, and if failbit is on in exceptions() the
caught exception is rethrown.
Returns:
*this.
27.6.2.5 Unformatted output functions [lib.ostream.unformatted]
1 Each formatted output function begins execution by constructing an
object of class sentry. If this object returns true, while converting
to a value of type bool, the function endeavors to generate the
requested output. In any case, the formatted output function ends by
destructing the sentry object , then returning the value specified for
the formatted output function.
basic_ostream<charT,traits>& put(char_type c);
Effects:
Inserts the character c, if possible.35)
_________________________
35) Note that this function is not overloaded on types signed char and
unsigned char.
2 Otherwise, calls setstate(badbit) (which may throw ios_base::failure
(_lib.iostate.flags_)).
Returns:
*this.
basic_ostream& write(const char_type* s, streamsize n);
Effects:
Obtains characters to insert from successive locations of an array
whose first element is designated by s.36) Characters are inserted
until either of the following occurs:
--n characters are inserted;
--inserting in the output sequence fails (in which case the function
calls setstate(badbit), which may throw ios_base::failure
(_lib.iostate.flags_)).
Returns:
*this.
basic_ostream& flush();
3 If rdbuf() is not a null pointer, calls rdbuf()->pubsync(). If that
function returns -1 calls setstate(badbit) (which may throw
ios_base::failure (_lib.iostate.flags_)).
Returns:
*this.
27.6.2.6 Standard basic_ostream manipulators [lib.ostream.manip]
namespace std {
template<class charT, class traits>
basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
}
Effects:
Calls os.put(traits::newline()), then os.flush().
Returns:
os.37)
_________________________
36) Note that this function is not overloaded on types signed char and
unsigned char.
37) The effect of executing cout << endl is to insert a newline char
acter in the output sequence controlled by cout, then synchronize it
with any external file with which it might be associated.
namespace std {
template<class charT, class traits>
basic_ostream<charT,traits>& ends(basic_ostream<charT,traits>& os);
}
Effects:
Inserts a null character into the output sequence: calls
os.put(traits::eos()).
Returns:
os.
namespace std {
template<class charT, class traits>
basic_ostream<charT,traits>& flush(basic_ostream<charT,traits>& os);
}
Effects:
Calls os.flush().
Returns:
os.
27.6.3 Standard manipulators [lib.std.manip]
1 The header <iomanip> defines a type and several related functions that
use this type to provide extractors and inserters that alter informa
tion maintained by class ios_base and its derived classes.
2 In describing functions declared in iomanip types smanip are n imple
mentation-defined function type (_dcl.fct_) returned by the standard
manipulators.
smanip resetiosflags(ios_base::fmtflags mask);
Returns:
smanip(f, mask), where f can be defined as:38)
template<class charT, class traits>
ios_base& f(ios_base& str, ios_base::fmtflags mask)
{ // reset specified flags
str.setf(ios_base::fmtflags(0), mask);
return str;
}
_________________________
38) The expression cin >> resetiosflags(ios_base::skipws) clears
ios_base::skipws in the format flags stored in the istream object cin
(the same as cin >> noskipws), and the expression cout << resetios
flags(ios_base::showbase) clears ios_base::showbase in the format
flags stored in the ostream object cout (the same as cout << noshow
base).
smanip setiosflags(ios_base::fmtflags mask);
Returns:
smanip(f,mask), where f can be defined as:
ios_base& f(ios_base& str, ios_base::fmtflags mask)
{ // set specified flags
str.setf(mask);
return str;
}
smanip setbase(int base);
Returns:
smanip(f, base), where f can be defined as:
ios_base& f(ios_base& str, int base)
{ // set basefield
str.setf(n == 8 ? ios_base::oct :
n == 10 ? ios_base::dec :
n == 16 ? ios_base::hex :
ios_base::fmtflags(0), ios_base::basefield);
return str;
}
smanip setfill(int c);
Returns:
smanip(f, c), where f can be defined as:
ios_base& f(ios_base& str, int c)
{ // set fill character
str.fill(c);
return str;
}
smanip setprecision(int n);
Returns:
smanip(f, n), where f can be defined as:
ios_base& f(ios_base& str, int n)
{ // set precision
str.precision(n);
return str;
}
smanip setw(int n);
Returns:
smanip(f, n), where f can be defined as:
ios_base& f(ios_base& str, int n)
{ // set width
str.width(n);
return str;
}
27.7 String-based streams [lib.string.streams]
1 The header <sstream> defines four template classes, and six types,
that associate stream buffers with objects of class basic_string, as
described in subclause _lib.string.classes_.
Header <sstream> synopsis
#include <streambuf>
#include <istream>
#include <ostream>
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_stringbuf;
typedef basic_stringbuf<char> stringbuf;
typedef basic_stringbuf<wchar_t> wstringbuf;
template <class charT, class traits = char_traits<charT> >
class basic_istringstream;
typedef basic_istringstream<char> istringstream;
typedef basic_istringstream<wchar_t> wistringstream;
template <class charT, class traits = char_traits<charT> >
class basic_ostringstream;
typedef basic_ostringstream<char> ostringstream;
typedef basic_ostringstream<wchar_t> wostringstream;
}
27.7.1 Template class basic_stringbuf [lib.stringbuf]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_stringbuf : public basic_streambuf<charT,traits> {
public:
// Types:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
// _lib.stringbuf.cons_ Constructors:
explicit basic_stringbuf(ios_base::openmode which
= ios_base::in | ios_base::out);
explicit basic_stringbuf(const basic_string<char_type>& str,
ios_base::openmode which
= ios_base::in | ios_base::out);
// _lib.stringbuf.members_ Get and set:
basic_string<char_type> str() const;
void str(const basic_string<char_type>& s);
protected:
// _lib.stringbuf.virtuals_ Overridden virtual functions:
virtual int_type underflow();
virtual int_type pbackfail(int_type c = traits::eof());
virtual int_type overflow (int_type c = traits::eof());
virtual pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out);
virtual pos_type seekpos(pos_type sp,
ios_base::openmode which
= ios_base::in | ios_base::out);
private:
// ios_base::openmode mode; exposition only
};
}
1 The class basic_stringbuf is derived from basic_streambuf to associate
possibly the input sequence and possibly the output sequence with a
sequence of arbitrary characters. The sequence can be initialized
from, or made available as, an object of class basic_string.
2 For the sake of exposition, the maintained data is presented here as:
--ios_base::openmode mode, has in set if the input sequence can be
read, and out set if the output sequence can be written.
27.7.1.1 basic_stringbuf constructors [lib.stringbuf.cons]
explicit basic_stringbuf(ios_base::openmode which =
ios_base::in | ios_base::out);
Effects:
Constructs an object of class basic_stringbuf, initializing the base
class with basic_streambuf() (_lib.streambuf.cons_), and initializ
ing mode with which.
Notes:
The function allocates no array object.
explicit basic_stringbuf(const basic_string<char_type>& str,
ios_base::openmode which = ios_base::in | ios_base::out);
Effects:
Constructs an object of class basic_stringbuf, initializing the base
class with basic_streambuf() (_lib.streambuf.cons_), and initializ
ing mode with which.
Postconditions:
str() == str. If str.size() > 0, sets the get and/or put pointers
as indicated in Table 9:
Table 9--str get/set areas
+------------------------------------------------------------------+
| Condition Setting |
+------------------------------------------------------------------+
|(which & ios_base::in) != 0 setg(str(),str(),str()+str.size()) |
+------------------------------------------------------------------+
|(which & ios_base::out) != 0 setp(str(),str(),str()+str.size()) |
+------------------------------------------------------------------+
27.7.1.2 Member functions [lib.stringbuf.members]
basic_string<char_type> str() const;
Returns:
The return value of this function are indicated in Table 10:
Table 10--str return values
+---------------------------------------------------------------------------+
| Condition Return Value |
+---------------------------------------------------------------------------+
|(mode & basic_ios::in) basic_string<char_type>(eback(),egptr()-eback()) |
|!= 0 and (gptr() != 0) |
+---------------------------------------------------------------------------+
|(mode & basic_ios::out) basic_string<char_type>(pbase(),pptr()-pbase()) |
|!= 0 and (pptr() != 0) |
+---------------------------------------------------------------------------+
|Otherwise basic_string<char_type>() |
+---------------------------------------------------------------------------+
void str(const basic_string<char_type>& s);
Effects:
If s.length() is zero, executes:
setg(0, 0, 0);
setp(0, 0);
Postcondition:
str() == s. If str.size() > 0, sets the get and/or put pointers as
indicated in Table 11:
Table 11--str get/set areas
+------------------------------------------------------------------+
| Condition Setting |
+------------------------------------------------------------------+
|(which & ios_base::in) != 0 setg(str(),str(),str()+str.size()) |
+------------------------------------------------------------------+
|(which & ios_base::out) != 0 setp(str(),str(),str()+str.size()) |
+------------------------------------------------------------------+
27.7.1.3 Overridden virtual functions [lib.stringbuf.virtuals]
int_type underflow();
Returns:
If the input sequence has a read position available, returns
traits::to_int_type(*gptr()).
Otherwise, returns traits::eof().
int_type pbackfail(int_type c = traits::eof());
+------- BEGIN BOX 33 -------+
Check vs. _lib.streambuf.virtuals_ and _lib.filebuf.virtuals_
+------- END BOX 33 -------+
Effects:
Puts back the character designated by c to the input sequence, if
possible, in one of three ways:
--If traits::eq_int_type(c,traits::eof()) returns false and if the
input sequence has a putback position available, and if
traits::eq(to_char_type(c),gptr()[-1]) returns true, assigns gptr()
- 1 to gptr().
Returns: c.
--If traits::eq_int_type(c,traits::eof()) returns false and if the
input sequence has a putback position available, and if mode &
ios_base::out is nonzero, assigns c to *--gptr().
Returns: c.
--If traits::eq_int_type(c,traits::eof()) returns true and if the
input sequence has a putback position available, assigns gptr() - 1
to gptr().
Returns: traits::not_eof(c).
Returns:
traits::eof() to indicate failure.
Notes:
If the function can succeed in more than one of these ways, it is
unspecified which way is chosen.
int_type overflow(int_type c = traits::eof());
Effects:
Appends the character designated by c to the output sequence, if
possible, in one of two ways:
--If traits::eq_int_type(c,traits::eof()) returns false and if either
the output sequence has a write position available or the function
makes a write position available (as described below), the function
calls sputc(c).
Signals success by returning c.
--If traits::eq_int_type(c,traits::eof()) returns true, there is no
character to append.
Signals success by returning a value other than traits::eof().
Notes:
The function can alter the number of write positions available as a
result of any call.
Returns:
traits::eof() to indicate failure.
1 [Note: The function can make a write position available only if (mode
& ios_base::out) != 0. To make a write position available, the func
tion reallocates (or initially allocates) an array object with a suf
ficient number of elements to hold the current array object (if any),
plus one additional write position. If (mode & ios_base::in) != 0,
the function alters the read end pointer egptr() to point just past
the new write position (as does the write end pointer epptr()).
--end note]
pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out);
+------- BEGIN BOX 34 -------+
Check vs. _lib.filebuf.virtuals_
+------- END BOX 34 -------+
Effects:
Alters the stream position within one of the controlled sequences,
if possible, as indicated in Table 12:
Table 12--seekoff positioning
+----------------------------------------------------------------------------------+
| Conditions Result |
+----------------------------------------------------------------------------------+
|(which & basic_ios::in) != 0 positions the input sequence |
+----------------------------------------------------------------------------------+
|(which & basic_ios::out) != 0 positions the output sequence |
+----------------------------------------------------------------------------------+
|Otherwise, |
|(which & (basic_ios::in | ba positions both the input and the output sequences |
|sic_ios::out)) == (ba |
|sic_ios::in | ba |
|sic_ios::out)) |
|and way == either ba |
|sic_ios::beg or ba |
|sic_ios::end |
+----------------------------------------------------------------------------------+
|Otherwise, the positioning operation fails. |
+----------------------------------------------------------------------------------+
2 For a sequence to be positioned, if its next pointer (either gptr() or
pptr()) is a null pointer, the positioning operation fails. Other
wise, the function determines newoff as indicated in Table 13:
Table 13--newoff values
+---------------------------------------------------------+
| Condition newoff Value |
+---------------------------------------------------------+
|way == basic_ios::beg 0 |
+---------------------------------------------------------+
|way == basic_ios::cur the next pointer minus the be |
| ginning pointer (xnext - xbeg). |
+---------------------------------------------------------+
|way == basic_ios::end the end pointer minus the be |
| ginning pointer (xend - xbeg) |
+---------------------------------------------------------+
|If (newoff + off) < 0, the positioning operation fails |
|or (xend - xbeg) < |
|(newoff + off) |
+---------------------------------------------------------+
3 Otherwise, the function assigns xbeg + newoff + off to the next
pointer xnext.
Returns:
pos_type(newoff), constructed from the resultant offset newoff (of
type off_type), that stores the resultant stream position, if
possible. If the positioning operation fails, or if the constructed
object cannot represent the resultant stream position, the object
stores an invalid stream position.
pos_type seekpos(pos_type sp, ios_base::openmode which
= ios_base::in | ios_base::out);
+------- BEGIN BOX 35 -------+
Check vs. _lib.filebuf.virtuals_
+------- END BOX 35 -------+
Effects:
Alters the stream position within one of the controlled sequences,
if possible, to correspond to the stream position stored in sp (as
described below).
--If (which & basic_ios::in) != 0, positions the input sequence.
--If (which & basic_ios::out) != 0, positions the output sequence.
--If the function positions neither sequence, the positioning opera
tion fails.
4 For a sequence to be positioned, if its next pointer (either gptr() or
pptr()) is a null pointer, the positioning operation fails. Other
wise, the function determines newoff from sp.offset():
--If newoff is an invalid stream position, has a negative value, or
has a value greater than (xend - xbeg), the positioning operation
fails.
--Otherwise, the function adds newoff to the beginning pointer xbeg
and stores the result in the next pointer xnext.
Returns:
pos_type(newoff), constructed from the resultant offset newoff (of
type off_type), that stores the resultant stream position, if possi
ble. If the positioning operation fails, or if the constructed
object cannot represent the resultant stream position, the object
stores an invalid stream position.
27.7.2 Template class basic_istringstream [lib.istringstream]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_istringstream : public basic_istream<charT,traits> {
public:
// Types:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
// _lib.istringstream.cons_ Constructors:
explicit basic_istringstream(ios_base::openmode which = ios_base::in);
explicit basic_istringstream(const basic_string<charT>& str,
ios_base::openmode which = ios_base::in);
// _lib.istringstream.members_ Members:
basic_stringbuf<charT,traits>* rdbuf() const;
basic_string<charT> str() const;
void str(const basic_string<charT>& s);
private:
// basic_stringbuf<charT,traits> sb; exposition only
};
}
1 The class basic_istringstream<charT,traits> supports reading objects
of class basic_string<charT,traits>. It uses a basic_stringbuf object
to control the associated storage. For the sake of exposition, the
maintained data is presented here as:
--sb, the stringbuf object.
27.7.2.1 basic_istringstream [lib.istringstream.cons]
constructors
explicit basic_istringstream(ios_base::openmode which = ios_base::in);
Effects:
Constructs an object of class basic_istringstream<charT,traits>,
initializing the base class with basic_istream(&sb) and initializing
sb with basic_stringbuf<charT,traits>(which))
(_lib.stringbuf.cons_).
explicit basic_istringstream(const basic_string<charT>& str,
ios_base::openmode which = ios_base::in);
Effects:
Constructs an object of class basic_istringstream<charT,traits>,
initializing the base class with basic_istream(&sb) and initializing
sb with basic_stringbuf<charT,traits>(str, which))
(_lib.stringbuf.cons_).
27.7.2.2 Member functions [lib.istringstream.members]
basic_stringbuf<charT,traits>* rdbuf() const;
Returns:
(basic_stringbuf<charT,traits>*)&sb.
basic_string<charT> str() const;
Returns:
rdbuf()->str().39)
void str(const basic_string<charT>& s);
Effects:
Calls rdbuf()->str(s).
27.7.2.3 Class basic_ostringstream [lib.ostringstream]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_ostringstream : public basic_ostream<charT,traits> {
public:
// Types:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
// _lib.ostringstream.cons_ Constructors/destructor:
explicit basic_ostringstream(ios_base::openmode which = ios_base::out);
explicit basic_ostringstream(const basic_string<charT>& str,
ios_base::openmode which = ios_base::out);
virtual ~basic_ostringstream();
// _lib.ostringstream.members_ Members:
basic_stringbuf<charT,traits>* rdbuf() const;
basic_string<charT> str() const;
void str(const basic_string<charT>& s);
private:
// basic_stringbuf<charT,traits> sb; exposition only
};
}
1 The class basic_ostringstream<charT,traits> supports writing objects
of class basic_string<charT,traits>. It uses a basic_stringbuf object
to control the associated storage. For the sake of exposition, the
maintained data is presented here as:
--sb, the stringbuf object.
27.7.2.4 basic_ostringstream [lib.ostringstream.cons]
constructors
explicit basic_ostringstream(ios_base::openmode which = ios_base::out);
Effects:
Constructs an object of class basic_ostringstream, initializing the
base class with basic_ostream(&sb) and initializing sb with
_________________________
39) rdbuf() is never NULL because it always returns the private ob
ject.
basic_stringbuf<charT,traits>(which)) (_lib.stringbuf.cons_).
explicit basic_ostringstream(const basic_string<charT>& str,
ios_base::openmode which = ios_base::out);
Effects:
Constructs an object of class basic_ostringstream<charT,traits>,
initializing the base class with basic_ostream(&sb) and initializing
sb with basic_stringbuf<charT,traits>(str, which))
(_lib.stringbuf.cons_).
27.7.2.5 Member functions [lib.ostringstream.members]
basic_stringbuf<charT,traits>* rdbuf() const;
Returns:
(basic_stringbuf<charT,traits>*)&sb.
basic_string<charT> str() const;
Returns:
rdbuf()->str().40)
void str(const basic_string<charT>& s);
Effects:
Calls rdbuf()->str(s).
27.7.3 Template class basic_stringstream [lib.stringstream]
namespace std {
template<class charT, class traits=char_traits<charT> >
class basic_stringstream
: public basic_iostream<charT,traits> {
public:
// Types
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
// constructors/destructors
explicit basic_stringstream(
ios_base::openmode which = ios_base::out|ios_base::in);
explicit basic_stringstream(
const basic_string<charT>& str,
ios_base::openmode which = ios_base::out|ios_base::in);
_________________________
40) rdbuf() is never NULL because it always returns the private ob
ject.
// Members:
basic_stringbuf<charT,traits>* rdbuf() const;
basic_string<charT> strs() const;
void str(const basic_string<charT>& str);
private:
// basic_stringbuf<charT, traits> sb; exposition only
};
1 The template class basic_stringstream<charT,traits> supports reading
and writing from objects of class basic_string<charT,traits>. It uses
a basic_stringbuf<charT,traits> object to control the associated
sequence. For the sake of exposition, the maintained data is pre
sented here as
--sb, the stringbuf object.
27.7.4 basic_stringstream constructors [lib.stringstream.cons]
explicit basic_stringstream(
ios_base::openmode which = ios_base::out|iosbase::in);
Effects:
Constructs an object of class basic_stringstream<charT,traits>, ini
tializing the base class with basic_iostream(&sb) and initializing
sb with basic_stringbuf<charT,traits>(which).
explicit basic_stringstream(
const basic_string<charT>& str,
ios_base::openmode which = ios_base::out|iosbase::in);
Effects:
Constructs an object of class basic_stringstream<charT,traits>, ini
tializing the base class with basic_iostream(&sb) and initializing
sb with basic_stringbuf<charT,traits>(str,which).
27.7.5 Member [functions]
basic_stringbuf<charT,traits>* rdbuf() const;
Returns:
&sb
basic_string<charT> str() const;
Returns:
rdbuf()->str().41)
_________________________
41) rdbuf() is never NULL because it always returns the private ob
ject.
void str(const basic_string<charT>& str);
Effects:
Calls rdbuf()->str(str).
27.8 File-based streams [lib.file.streams]
27.8.1 File streams [lib.fstreams]
1 The header <fstream> defines four class templates, and six types, that
associate stream buffers with files and assist reading and writing
files.
Header <fstream> synopsis
#include <streambuf>
#include <istream>
#include <ostream>
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_filebuf;
typedef basic_filebuf<char> filebuf;
typedef basic_filebuf<wchar_t> wfilebuf;
template <class charT, class traits = char_traits<charT> >
class basic_ifstream;
typedef basic_ifstream<char> ifstream;
typedef basic_ifstream<wchar_t> wifstream;
template <class charT, class traits = char_traits<charT> >
class basic_ofstream;
typedef basic_ofstream<char> ofstream;
typedef basic_ofstream<wchar_t> wofstream;
}
+------- BEGIN BOX 36 -------+
Issue: basic_filebuf<charT,traits> should be specified so that it
treats a file as a sequence of charT. Except for filebuf and wfilebuf
that implies it treats the file as binary.
+------- END BOX 36 -------+
2 In this subclause, the type name FILE refers to the type FILE defined
in <cstdio> (_lib.c.files_).42)
--File A File provides an external source/sink stream whose underlaid
character type is char (byte).43)
_________________________
42) In C FILE must be a typedef. In C++ it may be a typedef or other
type name.
43) A File is a sequence of multibyte characters. In order to provide
the contents as a wide character sequence, filebuf should convert be
--Multibyte character and Files A File provides byte sequences. So
the streambuf (or its derived classes) treats a file as the external
source/sink byte sequence. In a large character set environment,
multibyte character sequences are held in files. In order to pro
vide the contents of a file as wide character sequences, wide-
oriented filebuf, namely wfilebuf should convert wide character
sequences.
+------- BEGIN BOX 37 -------+
Change: The following "sentence" has been removed. Its original intent
was unclear. Because of necessity of the conversion between the
external source/sink streams and wide character sequences.
+------- END BOX 37 -------+
27.8.1.1 Template class basic_filebuf [lib.filebuf]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_filebuf : public basic_streambuf<charT,traits> {
public:
// Types:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
// _lib.filebuf.cons_ Constructors/destructor:
basic_filebuf();
virtual ~basic_filebuf();
// _lib.filebuf.members_ Members:
bool is_open() const;
basic_filebuf<charT,traits>* open(const char* s, ios_base::openmode mode);
basic_filebuf<charT,traits>* close();
protected:
// _lib.filebuf.virtuals_ Overridden virtual functions:
virtual int showmanyc();
virtual int_type underflow();
virtual int_type pbackfail(int_type c = traits::eof());
virtual int_type overflow (int_type c = traits::eof());
virtual basic_streambuf<charT,traits>*
setbuf(char_type* s, streamsize n);
virtual pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out);
virtual pos_type seekpos(pos_type sp, ios_base::openmode which
= ios_base::in | ios_base::out);
virtual int sync();
virtual void imbue(const locale& loc);
};
}
_________________________
tween wide character sequences and multibyte character sequences.
1 The class basic_filebuf<charT,traits> associates both the input
sequence and the output sequence with a file.
2 The restrictions on reading and writing a sequence controlled by an
object of class basic_filebuf<charT,traits> are the same as for read
ing and writing with the Standard C library FILEs.
3 In particular:
--If the file is not open for reading the input sequence cannot be
read.
--If the file is not open for writing the output sequence cannot be
written.
--A joint file position is maintained for both the input sequence and
the output sequence.
4 In order to support file I/O and multibyte/wide character conversion,
conversions are performed using getloc(). Specifically:
--when input is performed, bytes are read from the file and converted
to charT ``as if'' by using
use_facet<codecvt<char,charT,typename traits::state_type> >(get
loc())
+------- BEGIN BOX 38 -------+
Change: char_traits::state_type has been changed to traits::state_type
The former made no sense, the latter is sensible.
+------- END BOX 38 -------+
--when output is performed, charT's are converted to char ``as if'' by
using
use_facet<codecvt<charT,char,typename traits::state_type> >(get
loc()).
+------- BEGIN BOX 39 -------+
Change: char_traits::state_type has been changed to traits::state_type
The former made no sense, the latter is sensible.
+------- END BOX 39 -------+
27.8.1.2 basic_filebuf constructors [lib.filebuf.cons]
basic_filebuf();
Effects:
Constructs an object of class basic_filebuf<charT,traits>, initial
izing the base class with basic_streambuf<charT,traits>()
(_lib.streambuf.cons_).
Postcondition:
is_open() == false.
virtual ~basic_filebuf();
Effects:
Destroys an object of class basic_filebuf<charT,traits>. Calls
close().
27.8.1.3 Member functions [lib.filebuf.members]
bool is_open() const;
Returns:
true if the associated file is available and open.
basic_filebuf<charT,traits>* open(
const char* s,
ios_base::openmode mode );
Effects:
If is_open() == true, returns a null pointer. Otherwise, initial
izes the filebuf as required.
It then opens a file, if possible, whose name is the NTBS s (``as
if'' by calling ::fopen(s,modstr)).
The NTBS modstr is determined from mode & ~ios_base::ate as indi
cated in Table 14:
Table 14--File open modes
+---------------------------------------------+
| ios_base stdio equivalent |
| Value(s) |
+---------------------------------------------+
|in "r" |
|out | trunc "w" |
|out | app "a" |
|in | out "r+" |
|in | binary "rb" |
|out | trunc | binary "wb" |
|out | app | binary "ab" |
|in | out | trunc "w+" |
|in | out | app "a+" |
|in | out | binary "r+b" |
|in | out | trunc | binary "w+b" |
|in | out | app | binary "a+b" |
+---------------------------------------------+
+------- BEGIN BOX 40 -------+
Issue 27-803 The table omits many cases, including the common case of
out.
+------- END BOX 40 -------+
1 If the open operation succeeds and (mode & ios_base::ate) != 0, posi
tions the file to the end (``as if'' by calling
::fseek(file,0,SEEK_END)).44)
2 If the repositioning operation fails, calls close() and returns a null
pointer to indicate failure.
Returns:
this if successful, a null pointer otherwise.
basic_filebuf<charT,traits>* close();
Effects:
If is_open() == false, returns a null pointer. Otherwise, closes
the file (``as if'' by calling ::fclose(file)).45)
Returns:
this on success, a null pointer otherwise.
Postcondition:
is_open() == false.
27.8.1.4 Overridden virtual functions [lib.filebuf.virtuals]
int showmanyc();
Effects:
Behaves the same as basic_streambuf::showmanyc()
(_lib.streambuf.virtuals_).
Notes:
An implementation might well provide an overriding definition for
this function signature if it can determine that more characters can
be read from the input sequence.
int_type underflow();
Effects:
Behaves according to the description of
basic_streambuf<charT,traits>:: underflow(), with the specialization
that a sequence of characters is read from the input sequence ``as
_________________________
44) The macro SEEK_END is defined, and the function signatures
fopen(const char_type*, const char_type*) and fseek(FILE*, long, int)
are declared, in <cstdio> (_lib.c.files_).
45) The function signature fclose(FILE*) is declared, in <cstdio>
(_lib.c.files_).
if'' by reading from the associated file into an internal buffer (
from_buf) and then ``as if'' doing
char from_buf[FSIZE];
char* from_end;
charT to_buf[TSIZE];
charT* to_end;
codecvt_base::result r =
use_facet<codecvt<char,charT,typename typename traits::state_type> >(getloc()).
convert(st,from_buf,from_buf+FSIZE,from_end,
to_buf, to_buf+TSIZE, to_end);
+------- BEGIN BOX 41 -------+
Change: typename traits::state_type replaces char_traits::state_type"
in the above code.
+------- END BOX 41 -------+
This must be done in such a way that the class can recover the posi
tion ( fpos_t) corresponding to each character between to_buf and
to_end. If the value of r indicates that convert() ran out of space
in to_buf, retry with a larger to_buf.
int_type pbackfail(int_type c = traits::eof());
+------- BEGIN BOX 42 -------+
Check vs. _lib.streambuf.virtuals_.
+------- END BOX 42 -------+
Effects:
Puts back the character designated by c to the input sequence, if
possible, in one of three ways:
--If traits::eq_int_type(c,traits::eof()) returns false and if the
function makes a putback position available and if
traits::eq(to_char_type(c),gptr()[-1]) returns true, decrements the
next pointer for the input sequence, gptr().
Returns: c.
--If traits::eq_int_type(c,traits::eof()) returns false and if the
function makes a putback position available, and if the function is
permitted to assign to the putback position, decrements the next
pointer for the input sequence, and stores c there.
Returns: c.
--If traits::eq_int_type(c,traits::eof()) returns true and if either
the input sequence has a putback position available or the function
makes a putback position available, decrements the next pointer for
the input sequence, gptr().
Returns: traits::not_eof(c).
Returns:
traits::eof() to indicate failure.
Notes:
If is_open() == false, the function always fails.
The function does not put back a character directly to the input
sequence.
If the function can succeed in more than one of these ways, it is
unspecified which way is chosen. The function can alter the number
of putback positions available as a result of any call.
int_type overflow(int_type c = traits::eof());
Effects:
Behaves according to the description of
basic_streambuf<charT,traits>::overflow(c), except that the behavior
of ``consuming characters'' is performed by first coverting ``as
if'' by:
charT* b = pbase();
charT* p = pptr();
charT* end;
char buf[BSIZE];
char* ebuf;
codecvt_base::result r =
use_facet<codecvt<charT, char, typename traits::state_type> >(getloc()).
convert(st,b(),p(),end,buf,buf+BSIZE,ebuf);
+------- BEGIN BOX 43 -------+
Change: traits::state_type replaces char_traits::state_type in the
above code.
+------- END BOX 43 -------+
and then
--If r == codecvt_base::error then fail.
--If r == codecvt_base::noconv then output chararcters from b upto
(and not including) p.
--If r == codecvt_base::partial then output to the file characters
from buf upto ebuf, and repeat using characters from end to p. If
output fails, fail (without repeating).
--Otherwise output from buf to ebuf, and fail if output fails. At
this point if b != p and b == end ( buf isn't large enough) then
increase BSIZE and repeat from the beginning.
Returns:
traits::eof() to indicate failure. If is_open() == false, the func
tion always fails.
basic_streambuf* setbuf(char_type* s, int n);
+------- BEGIN BOX 44 -------+
Issue 27-809: Check vs. _lib.streambuf.virtuals_ and
_lib.stringbuf.virtuals_.
+------- END BOX 44 -------+
pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out);
Effects:
The current state is determined as follows: If the the last opera
tion was overflow(), the current state is obtained by combining the
shiftstate contained in st with the current position ( fpos_t) of
the file. If the last operation was underflow(), the shiftstate and
file position are determined (according to whatever means they were
saved by underflow()) as corresponding to pptr().
Then, alters the stream position within the controlled sequences, if
possible, as described below.
If is_open() == false, the positioning operation fails. Otherwise,
repositions within the associated file (``as if'' by calling
::fseek(file,off,whence).46)
Notes:
The function determines one of three values for the argument whence,
of type int, as indicated in Table 15:
Table 15--seekoff effects
+----------------------------------+
| way Value stdio Equivalent |
+----------------------------------+
|basic_ios::beg SEEK_SET |
|basic_ios::cur SEEK_CUR |
|basic_ios::end SEEK_END |
+----------------------------------+
+------- BEGIN BOX 45 -------+
Change: The previous paragraph was in a [ ] note, but it is clearly
needed and substantive.
+------- END BOX 45 -------+
The function extracts the conversion state from off by means of
get_offstate() to reset the rdstate() member.
+------- BEGIN BOX 46 -------+
Issue: The previous sentence contradict earlier paragraph. And it
_________________________
46) The macros SEEK_SET, SEEK_CUR, and SEEK_END are defined, and the
function signature fseek(FILE*, long, int) is declared, in <cstdio>
(_lib.c.files_).
makes no sense as conversion states are not embedded in offsets. And
there is no get_offstate function.
+------- END BOX 46 -------+
Returns:
a newly constructed pos_type object that stores the resultant stream
position, if possible. If the positioning operation fails, or if
the object cannot represent the resultant stream position, returns
an invalid stream position (_lib.iostreams.pos.t_).
pos_type seekpos(pos_type sp, ios_base::openmode which
= ios_base::in | ios_base::out);
+------- BEGIN BOX 47 -------+
Issue 27-815 [TBS] Check vs. _lib.streambuf.virtuals_ and
_lib.stringbuf.virtuals_.
+------- END BOX 47 -------+
int sync();
+------- BEGIN BOX 48 -------+
Issue 27-811 [TBS] Check vs. _lib.streambuf.virtuals_.
+------- END BOX 48 -------+
void imbue(const locale& loc);
27.8.1.5 Template class basic_ifstream [lib.ifstream]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_ifstream : public basic_istream<charT,traits> {
public:
// Types:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
// _lib.ifstream.cons_ Constructors:
basic_ifstream();
explicit basic_ifstream(const char* s, openmode mode = in);
// _lib.ifstream.members_ Members:
basic_filebuf<charT,traits>* rdbuf() const;
bool is_open();
void open(const char* s, openmode mode = in);
void close();
private:
// basic_filebuf<charT,traits> sb; exposition only
};
}
1 The class basic_ifstream<charT,traits> supports reading from named
files. It uses a basic_filebuf<charT,traits> object to control the
associated sequence. For the sake of exposition, the maintained data
is presented here as:
--sb, the filebuf object.
27.8.1.6 basic_ifstream constructors [lib.ifstream.cons]
basic_ifstream();
Effects:
Constructs an object of class basic_ifstream<charT,traits>, initial
izing the base class with basic_istream(&sb) and initializing sb
with basic_filebuf<charT,traits>()) (_lib.istream.cons_,
_lib.filebuf.cons_).
explicit basic_ifstream(const char* s, openmode mode = in);
Effects:
Constructs an object of class basic_ifstream, initializing the base
class with basic_istream(&sb) and initializing sb with
basic_filebuf<charT,traits>()) (_lib.istream.cons_,
_lib.filebuf.cons_), then calls rdbuf()->open(s,mode).47)
27.8.1.7 Member functions [lib.ifstream.members]
basic_filebuf<charT,traits>* rdbuf() const;
Returns:
(basic_filebuf<charT,traits>*)&sb.
bool is_open();
Returns:
rdbuf()->is_open().48)
_________________________
47) rdbuf() is never NULL because it always returns the private ob
ject.
48) rdbuf() is never NULL because it always returns the private ob
ject.
void open(const char* s, openmode mode = in);
Effects:
Calls rdbuf()->open(s,mode). If is_open() returns false, calls set
state(failbit) (which may throw ios_base::failure
(_lib.iostate.flags_)).
void close();
Effects:
Calls rdbuf()->close() and, if that function returns false, calls
setstate(failbit) (which may throw ios_base::failure
(_lib.iostate.flags_)).
27.8.1.8 Template class basic_ofstream [lib.ofstream]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_ofstream : public basic_ostream<charT,traits> {
public:
// Types:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
// _lib.ofstream.cons_ Constructors:
basic_ofstream();
explicit basic_ofstream(const char* s, openmode mode = out | trunc);
// _lib.ofstream.members_ Members:
basic_filebuf<charT,traits>* rdbuf() const;
bool is_open();
void open(const char* s, ios_base::openmode mode = out | trunc);
void close();
private:
// basic_filebuf<charT,traits> sb; exposition only
};
}
1 The class basic_ofstream<charT,traits> supports writing to named
files. It uses a basic_filebuf<charT,traits> object to control the
associated sequence. For the sake of exposition, the maintained data
is presented here as:
--sb, the filebuf object.
27.8.1.9 basic_ofstream constructors [lib.ofstream.cons]
basic_ofstream();
Effects:
Constructs an object of class basic_ofstream<charT,traits>,
initializing the base class with basic_ostream(&sb) and initializing
sb with basic_filebuf<charT,traits>()) (_lib.ostream.cons_,
_lib.filebuf.cons_).
explicit basic_ofstream(const char* s, openmode mode = out);
Effects:
Constructs an object of class basic_ofstream<charT,traits>, initial
izing the base class with basic_ostream(&sb) and initializing sb
with basic_filebuf<charT,traits>()) (_lib.ostream.cons_,
_lib.filebuf.cons_), then calls rdbuf()->open(s, mode).49)
27.8.1.10 Member functions [lib.ofstream.members]
basic_filebuf<charT,traits>* rdbuf() const;
Returns:
(basic_filebuf<charT,traits>*)&sb.
bool is_open();
Returns:
rdbuf()->is_open().
void open(const char* s, openmode mode = out);
Effects:
Calls rdbuf()->open(s,mode). If is_open() is then false, calls set
state(failbit) (which may throw ios_base::failure
(_lib.iostate.flags_)).
void close();
Effects:
Calls rdbuf()->close() and, if that function fails (returns a null
pointer), calls setstate(failbit) (which may throw ios_base::failure
(_lib.iostate.flags_)).
27.8.1.11 Template class basic_fstream [lib.fstream]
namespace std {
template<clas charT, class traits=ios_ytraits<charT> >
class basic_fstream
: public basic_iostream<charT,traits> {
_________________________
49) rdbuf() is never NULL because it always returns the private ob
ject.
public:
// Types
typedef charT char_type;
typedef typename traits::int_type ins_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
// constructors/destructor
basic_fstream();
explicit basic_fstream(
const char* s,
ios_base::openmode mode = ios_base::in|ios_base::out);
// Members:
basic_filebuf<charT,traits>* rdbuf() const;
bool is_open();
void open(
const char* s,
ios_base::openmode mode = ios_base::in|ios_base::out);
void close();
private:
// basic_filebuf<charT,traits> sb; exposition only
};
1 The template class basic_fstream<charT,traits> supports reading and
writing from named files. It uses a basic_filebuf<charT,traits>object
For the sake of exposition, the maintained data is presented here as:
--sb, the basic_filebuf object.
27.8.1.12 basic_fstream constructors [lib.fstream.cons]
basic_fstream();
Effects:
Constructs an object of class basic_fstream<charT,traits>, initial
izing the base class with basic_iostream(&sb) and initializing sb
with basic_filebuf<charT,traits>().
explicit basic_fstream(const char* s, ios_base::openmode mode);
Effects:
Construcs an object of class basic_fstream<charT,traits>, initializ
ing the base class with basic_iostream(&sb) and initializing sb with
basic_filebuf<charT,traits>(). Then calls sb.open(s,mode).
27.8.1.13 Member functions [lib.fstream.members]
basic_filebuf<charT,traits>*( rdbuf() const;
Returns:
&sb
bool is_open();
Returns:
rdbuf()->is_open().
void open(const char* s, ios_base::openmode mode);
Effects:
Calls rdbuf()->open(s,mode), then if is_open() returns false, calls
setstate(failbif) (which may throw ios_base::failure
(_lib.iostate.flags_) )
void close();
Effects:
Calls rdbuf()->close() and, if that function returns false, calls
setstate(failbif) (which may throw ios_base::failure
(_lib.iostate.flags_) )
27.8.2 C Library files [lib.c.files]
1 Table 16 describes header <cstdio>.
Table 16--Header <cstdio> synopsis
+-------------------------------------------------------------------------------+
| Type Name(s) |
+-------------------------------------------------------------------------------+
|Macros: |
|BUFSIZ L_tmpnam SEEK_SET TMP_MAX |
|EOF NULL <cstdio> stderr _IOFBF |
|FILENAME_MAX SEEK_CUR stdin _IOLBF |
|FOPEN_MAX SEEK_END stdout _IONBF |
+-------------------------------------------------------------------------------+
|Types: FILE fpos_t size_t <cstdio> |
+-------------------------------------------------------------------------------+
|Functions: |
|clearerr fgets fscanf gets rewind |
|fclose fopen fseek perror scanf tmpnam |
|feof fprintf fsetpos printf setbuf ungetc |
|ferror fputc ftell putc setvbuf vprintf |
|fflush fputs fwrite puts sprintf vfprintf |
|fgetc fread getc remove sscanf vsprintf |
|fgetpos freopen getchar putchar rename tmpfile |
+-------------------------------------------------------------------------------+
SEE ALSO: ISO C subclause 7.9, Amendment 1 subclause 4.6.2.