|
|
#include <iostream.h>typedef long streamoff, streampos; class ios { public: enum seek_dir { beg, cur, end }; enum open_mode { in, out, ate, app, trunc, nocreate, noreplace }; 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 }; // and lots of other stuff, see ios(C++) ... } ;
class ostream : public ios { public: ostream(streambuf*); ostream& flush(); int opfx(); ostream& put(char); ostream& seekp(streampos); ostream& seekp(streamoff, seek_dir); streampos tellp(); ostream& write(const char* ptr, int n); ostream& write(const unsigned char* ptr, int n); ostream& operator<<(const char*); ostream& operator<<(char); ostream& operator<<(short); ostream& operator<<(int); ostream& operator<<(long); ostream& operator<<(float); ostream& operator<<(double); ostream& operator<<(unsigned char); ostream& operator<<(unsigned short); ostream& operator<<(unsigned int); ostream& operator<<(unsigned long); ostream& operator<<(void*); ostream& operator<<(streambuf*); ostream& operator<<(ostream& (*)(ostream&)); ostream& operator<<(ios& (*)(ios&)); };
class ostream_withassign { ostream_withassign(); istream& operator=(istream&); istream& operator=(streambuf*); };extern ostream_withassign cout; extern ostream_withassign cerr; extern ostream_withassign clog;
ostream& endl(ostream&) ; ostream& ends(ostream&) ; ostream& flush(ostream&) ; ios& dec(ios&) ; ios& hex(ios&) ; ios& oct(ios&) ;
ostream
s support insertion (storing) into a streambuf
.
These are commonly referred to as output operations.
The ostream
member functions and related functions
are described below.
In the following descriptions, assume:
ostream
ostream_withassign
ostream*
char
char*
or unsigned char*
streambuf*
int
s
streampos
streamoff
seek_dir
ostream& (*)(ostream&)
ostream(
sb)
ios
state variables and associates
buffer sb with the ostream
.
ostream_withassign()
cout
, for example) to be used before it is constructed,
provided it is assigned to first.
->rdbuf()
with swa and initializes the entire
state of outswa.
.opfx()
.tie()
is non-null, it is flushed.
Returns non-zero except when outs's error state is nonzero.
osfx()
ios::unitbuf
is set,
osfx()
flushes the ostream
.
If ios::stdio
is set,
osfx()
flushes stdout and stderr.
osfx()
is called by all predefined inserters,
and should be called by user-defined inserters as well,
after any direct manipulation of the streambuf
.
It is not called by the binary output functions.
<<
x.opfx()
and if that returns 0, does nothing.
Otherwise inserts a sequence of characters
representing x into outs.rdbuf()
.
Errors are indicated by setting
the error state of outs.
outs is always returned.
x is converted into a sequence of characters (its representation) according to rules that depend on x's type and outs's format state flags and variables (see ios(C++)). Inserters are defined for the following types, with conversion rules as described below:
char*
char
and unsigned char
ios::dec
, ios::oct
, or ios::hex
, respectively,
is set in ios
's format flags.
If none of those flags are set, conversion defaults to decimal.
If x is zero, the representation is a single
zero character (0).
If x is negative, decimal conversion converts it to a minus
sign (-) followed by decimal digits.
If x is positive and ios::showpos
is set,
decimal conversion converts it to a plus sign (+)
followed by decimal digits.
The other conversions treat all values as unsigned.
If ios::showbase
is set in ios's format flags,
the hexadecimal representation contains
``0x'' before the hexadecimal digits,
or ``0X'' if ios::uppercase
is set.
If ios::showbase
is set,
the octal representation contains a leading 0.
void*
ios::showbase
were set.
float
, double
.precision()
, outs.width()
and outs's
format flags ios::scientific
, ios::fixed
, and
ios::uppercase
.
(See
ios(C++).)
The default value for outs.precision()
is 6.
If neither ios::scientific
nor ios::fixed
is set,
either fixed or scientific notation is chosen
for the representation, depending on the value of x.
char
, unsigned char
After the representation is determined, padding occurs.
If outs.width()
is greater than 0
and the representation contains fewer than outs.width()
characters, then enough outs.fill()
characters are added
to bring the total number of characters to ios.width()
.
If ios::left
is set in ios
's format flags,
the sequence is left-adjusted,
that is, characters are added after the characters determined above.
If ios::right
is set,
the padding is added before the characters determined above.
If ios::internal
is set,
the padding is added after any leading sign or base indication
and before the characters that represent the value.
ios.width()
is reset to 0, but all other format
variables are unchanged.
The resulting sequence (padding plus representation)
is inserted into outs.rdbuf()
.
<<
sb.opfx()
returns non-zero,
the sequence of characters that can be fetched from sb
are inserted into outs.rdbuf()
.
Insertion stops when no more characters can be fetched from sb.
No padding is performed.
Always returns outs.
&
outs.put(c)
.rdbuf()
.
Sets the error state if the insertion fails.
&
outs.write(
s,
n)
.rdbuf()
.
These characters may include zeros (i.e., s need not be
a null terminated string).
&
outs.flush()
flush()
causes any characters that may have been stored but not yet consumed
to be consumed by calling outs.rdbuf()->sync
.
<<manip
manip(
outs)
.
Syntactically this looks like an insertion
operation, but semantically it does an arbitrary operation
rather than converting manip
to a sequence of characters as
do the insertion operators.
Predefined manipulators are described below.
&
ins.seekp(
off,
dir)
.rdbuf()
's put pointer.
See
sbuf.pub(C++)
for a discussion of positioning.
=&
outs.seekp(
pos)
.rdbuf()
's put pointer.
See
sbuf.pub(C++)
for a discussion of positioning.
=
outs.tellp()
.rdbuf()
's put pointer.
See
sbuf.pub(C++)
for a discussion of positioning.
<<endl
<<ends
<<flush
<<dec
<<hex
<<oct