|
|
#include <iostream.h>class ios { public: enum io_state { goodbit=0, eofbit, failbit, badbit }; enum open_mode { in, out, ate, app, trunc, nocreate, noreplace }; enum seek_dir { beg, cur, end }; /* flags for controlling format */ enum { skipws=01, left=02, right=04, internal=010, dec=020, oct=040, hex=0100, showbase=0200, showpoint=0400, uppercase=01000, showpos=02000, scientific=04000, fixed=010000, unitbuf=020000, stdio=040000 }; static const long basefield; /* dec|oct|hex */ static const long adjustfield; /* left|right|internal */ static const long floatfield; /* scientific|fixed */ public: ios(streambuf*);
int bad(); static long bitalloc(); void clear(int state =0); int eof(); int fail(); char fill(); char fill(char); long flags(); long flags(long); int good(); long& iword(int); int operator!(); operator void*(); int precision(); int precision(int); streambuf* rdbuf(); void* & pword(int); int rdstate(); long setf(long setbits, long field); long setf(long); static void sync_with_stdio(); ostream* tie(); ostream* tie(ostream*); long unsetf(long); int width(); int width(int); static int xalloc(); protected: ios(); init(streambuf*); private: ios(ios&); void operator=(ios&) ; };
/* Manipulators */ ios& dec(ios&) ; ios& hex(ios&) ; ios& oct(ios&) ; ostream& endl(ostream& i) ; ostream& ends(ostream& i) ; ostream& flush(ostream&) ; istream& ws(istream&) ;
ios
provide a high level interface that
supports transferring
formatted and unformatted information into and out of
streambuf
s.
This manual page describes the operations common to both input
and output.
Several enumerations are declared in class ios
,
open_mode
, io_state
, seek_dir
,
and format flags,
to avoid polluting the global name space.
The io_state
s are described on this manual page
under ``Error States.''
The format fields are also described on this page,
under ``Formatting.''
The open_mode
s are described in detail in
fstream(C++)
under open()
.
The seek_dir
s are described in
sbuf.pub(C++)
under seekoff()
.
In the following descriptions assume:
ios
s
ios&
ios*
int
s
long
s
char
s
ostream*
s
streambuf*
streampos
streamoff
seek_dir
int
representing an open_mode
ios& (*)(ios&)
void*&
ios(
sb)
streambuf
denoted by sb
becomes the streambuf
associated with the constructed ios
.
If sb is null, the effect is undefined.
ios(
sr)
=
sios
s is not well-defined in general,
therefore the constructor and assignment operators are private
so that the compiler will complain about attempts
to copy ios
objects.
Copying pointers to iostream
s
is usually what is desired.
ios()
init(
sb)
ios
is now inherited as a virtual base class,
a constructor with no arguments must be used.
This constructor is declared protected.
Therefore ios::init(streambuf*)
is declared protected and
must be used for initialization of derived classes.
ios
has an internal error state (which is a collection
of the bits declared as io_state
s).
Members related to the error state are:
.rdstate()
.clear(
i)
ios::hardfail
(this bit
is not for public access).
To set a bit without clearing previously set bits requires something
like s.clear(ios::badbit|s.rdstate())
.
.good()
.eof()
eofbit
is set in the error state,
zero otherwise.
Normally this bit is set when an end-of-file has been encountered
during an extraction.
.fail()
badbit
or failbit
is set in the error state, zero otherwise.
Normally this indicates that some extraction or conversion has failed,
but the stream is still usable.
That is, once the failbit
is cleared,
I/O on s can usually continue.
.bad()
badbit
is set in the error state,
zero otherwise.
This usually indicates that some operation on s.rdbuf()
has failed,
a severe error, from which recovery is probably impossible.
That is, it will probably be impossible to continue I/O operations
on s.
ios
:
operator!()
and operator void*()
.
The latter converts an ios
to a pointer so that it can be
compared to zero.
The conversion will return 0 if failbit
or badbit
is set in the error state,
and will return a pointer value otherwise.
This pointer is not meant to be used.
This allows one to write expressions such as:
if ( cin ) ...The ``!'' operator returns non-zero ifif ( cin >> x ) ...
failbit
or badbit
is set in the error state,
which allows expressions like the following to be used:
if ( !cout ) ...
ios
has a
format state
that is used by input and output operations to control the
details of formatting operations.
For other operations the format state has no particular effect
and its components may be set and examined arbitrarily by user code.
Most formatting details are controlled by using the
flags()
, setf()
, and unsetf()
functions
to set the following flags,
which are declared in an enumeration in class ios
.
Three other components of the format state are controlled separately
with the functions fill()
, width()
, and precision()
.
skipws
skipws
is set, whitespace will be skipped on input.
This applies to scalar extractions.
When skipws
is not set, whitespace is not skipped
before the extractor begins conversion.
If skipws
is not
set and a zero length field is encountered, the extractor will signal
an error. Additionally, the arithmetic extractors will signal an
error if skipws
is not set and a whitespace is encountered.
left
right
internal
left
is set, the value is left-adjusted,
that is, the fill character is added after the value.
When right
is set, the value is right-adjusted,
that is, the fill character is added before the value.
When internal
is set, the fill character is added after
any leading sign or base indication, but before the value.
Right-adjustment is the default if none of these flags is set.
These fields are collectively identified by the static member,
ios::adjustfield
.
The fill character is controlled by the fill()
function,
and the width of padding is controlled by the width()
function.
dec
oct
hex
dec
is set,
but if oct
or hex
is set,
conversions are done in octal or hexadecimal, respectively.
If none of these is set, insertions are in decimal,
but extractions are interpreted according to the C++ lexical conventions
for integral constants.
These fields are collectively identified by the static member,
ios::basefield
.
The manipulators hex
, dec
, and oct
can also be used to set the conversion base; see ``Built-in manipulators''
below.
showbase
showbase
is set,
insertions will be converted to an external form that can be read
according to the C++ lexical conventions for integral constants.
showbase
is unset by default.
showpos
showpos
is set, then a ``+'' will be inserted
into a decimal conversion of a positive integral value.
uppercase
uppercase
is set, then an upper case ``X'' will be used
for hexadecimal conversion when showbase
is set,
or an upper case ``E'' will be used
to print floating point numbers in scientific notation.
showpoint
showpoint
is set, trailing zeros and decimal points
appear in the result of a floating point conversion.
scientific
fixed
scientific
is set, the value is converted using scientific
notation, where there is one digit before the decimal point
and the number of digits after it is equal to the precision
(see below), which is six by default.
An upper case ``E'' will introduce the exponent if uppercase
is set, a lower case ``e'' will appear otherwise.
If fixed
is set, the value is converted to decimal notation
with precision
digits after the decimal point,
or six by default.
If neither scientific
nor fixed
is set,
then the value will be converted using either notation,
depending on the value; scientific notation will be used
if the exponent resulting from the conversion
is less than -4 or greater than or equal to precision digits.
Otherwise the value will be converted to decimal notation with
precision
digits total.
If showpoint
is not set,
trailing zeroes are removed from the result
and a decimal point appears only if it is followed by a digit.
scientific
and fixed
are collectively identified
by the static member ios::floatfield
.
unitbuf
ostream::osfx()
after each insertion.
Unit buffering provides a compromise between buffered output
and unbuffered output.
Performance is better under unit buffering than unbuffered output,
which makes a system call for each character output.
Unit buffering makes a system call for each insertion operation,
and doesn't require the user to call ostream::flush()
.
stdio
ostream::osfx()
after each insertion. However, the flush will not be in
effect without a call to ios::sync_with_stdio()
.
The following functions use and set the format flags and variables.
.fill(
c)
width()
, below).
The default fill or padding character is a space.
The positioning of the fill character is determined by
the right
, left
, and internal
flags; see above.
A parameterized manipulator, setfill
, is also available for
setting the fill character; see
manip(C++).
.fill()
.flags()
.flags(f)
.precision(
i)
precision
format state variable to i
and returns the previous value.
This variable controls the number of significant digits inserted
by the floating point inserter.
The default is 6.
A parameterized manipulator, setprecision
, is also available for
setting the precision; see
manip(C++).
.precision()
precision
format state variable.
.setf(
b)
setiosflags
, performs the same function;
see
manip(C++).
.setf(
b,
f)
hex
,
one could write:
s.setf(ios::hex,ios::basefield)
.
ios::basefield
specifies the conversion base bits
as candidates for change, and ios::hex
specifies the new value.
s.setf(0,
f)
will clear all the bits specified by f,
as will a parameterized manipulator, resetiosflags
;
see
manip(C++).
.unsetf(
b)
.width(
i)
setw
, is also available for
setting the width; see
manip(C++).
.width()
ios
can be used as a base class for derived classes that
require additional format flags or variables.
The iostream library provides several functions to do this.
The two static member functions ios::xalloc
and ios::bitalloc
,
allow several such classes to be used together without interference.
ios::bitalloc()
long
with a single, previously unallocated, bit set.
This allows users who need an additional flag to acquire one,
and pass it as an argument to ios::setf()
, for example.
If it returns 0, all allocatable bits have been used.
ios::xalloc()
s.iword(
i)
ios::xalloc
,
iword()
returns a reference to the ith user-defined word.
s.pword(
i)
ios::xalloc
,
pword()
returns a reference to the ith user-defined word.
pword()
is the same as iword()
except that it is typed differently.
.rdbuf()
streambuf
associated with
s when s was constructed.
ios::sync_with_stdio()
cin
, cout
, cerr
,
clog
) to be streams using stdiobuf
s.
After that, input and output using these streams may
be mixed with input and output using the corresponding FILE
s
(stdin, stdout, and stderr)
and will be properly synchronized.
sync_with_stdio()
makes cout
and cerr
unit buffered
(see ios::unitbuf
and ios::stdio
above).
Invoking sync_with_stdio()
degrades performance a variable amount,
depending on the length of the strings being inserted
(shorter strings incur a larger performance hit).
.tie(
osp)
tie
variable to osp, and returns its previous value.
This variable supports automatic ``flushing'' of ios
s.
If the tie
variable is non-null and an ios
needs more characters
or has characters to be consumed,
the ios
pointed at by the tie variable is flushed.
By default, cin
is tied initially to cout
so that attempts to get more characters from standard input
result in flushing standard output.
Additionally, cerr
and clog
are tied to cout
by default.
For other ios
s, the tie variable is set to zero by default.
.tie()
tie
variable.
ios&
,
an istream&
, or an ostream&
and return their argument; see
manip(C++))
are:
<<dec
sr>>dec
<<hex
sr>>hex
<<oct
sr>>oct
>>ws
<<endl
<<ends
<<flush
outs
.
See
ostream(C++).
Several parameterized manipulators that operate on ios
objects are described in
manip(C++):
setw
, setfill
, setprecision
, setiosflags
,
and resetiosflags
.
The streambuf
associated with an ios
may be manipulated
by other methods than through the ios
.
For example, characters may
be stored in a queuelike streambuf
through an ostream
while they are being fetched through an istream
.
Or for efficiency some
part of a program may choose to do streambuf
operations
directly rather than through the ios
.
In most cases
the program does not have to worry about this possibility, because
an ios
never saves information about the internal state
of a streambuf
.
For example, if the streambuf
is
repositioned between extraction operations the extraction (input)
will proceed normally.
sync_with_stdio
is a wart.
The old stream package did this as a default, but in the iostream package
unbuffered stdiobuf
s are too inefficient to be the default.
The stream package had a constructor that took a FILE*
argument.
This is now replaced by stdiostream
.
It is not declared even as an obsolete form to avoid
having iostream.h
depend on stdio.h.
The old stream package allowed copying of streams.
This is disallowed by the iostream package.
However, objects of type istream_withassign
,
ostream_withassign
, and iostream_withassign
can be assigned to.
Old code using copying can usually be
rewritten to use pointers or these classes.
(The standard streams cin
, cout
, cerr
,
and clog
are members of ``withassign'' classes,
so they can be assigned to, as in
cin = inputfstream
.)