|
|
#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 }; /* 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 }; // and lots of other stuff, see ios(C++) ... } ;
class istream : public ios { public: istream(streambuf*); int gcount(); istream& get(char* ptr, int len, char delim='\n'); istream& get(unsigned char* ptr, int len, char delim='\n');
istream& get(unsigned char&); istream& get(char&); istream& get(streambuf& sb, char delim ='\n'); int get(); istream& getline(char* ptr, int len, char delim='\n'); istream& getline(unsigned char* ptr, int len, char delim='\n'); istream& ignore(int len=1,int delim=EOF); int ipfx(int need=0); int peek(); istream& putback(char); istream& read(char* s, int n); istream& read(unsigned char* s, int n); istream& seekg(streampos); istream& seekg(streamoff, seek_dir); int sync(); streampos tellg();
istream& operator>>(char*); istream& operator>>(char&); istream& operator>>(short&); istream& operator>>(int&); istream& operator>>(long&); istream& operator>>(float&); istream& operator>>(double&); istream& operator>>(unsigned char*); istream& operator>>(unsigned char&); istream& operator>>(unsigned short&); istream& operator>>(unsigned int&); istream& operator>>(unsigned long&); istream& operator>>(streambuf*); istream& operator>>(istream& (*)(istream&)); istream& operator>>(ios& (*)(ios&)); };
class istream_withassign : public istream { istream_withassign(); istream& operator=(istream&); istream& operator=(streambuf*); };
extern istream_withassign cin;
istream& ws(istream&) ; ios& dec(ios&) ; ios& hex(ios&) ; ios& oct(ios&) ;
istream
s support interpretation of characters
fetched from an associated streambuf
.
These are commonly referred to as input or extraction operations.
The istream
member functions and related functions
are described below.
In the following descriptions assume that
istream
istream_withassign
istream*
char&
char
char*
or unsigned char*
streambuf&
int
s
streampos
streamoff
seek_dir
istream& (*)(istream&)
istream(
sb)
ios
state variables and associates
buffer sb with the istream
.
istream_withassign()
->rdbuf()
with inswa and initializes the entire
state of inswa.
.ipfx(
need)
ios
tied to ins
is flushed (see the description ios::tie()
in
ios(C++)).
Flushing is considered necessary if either need==0
or if there
are fewer than need characters immediately available.
If ios::skipws
is set in ins.flags()
and need is zero, then leading whitespace characters are
extracted from ins.
ipfx()
returns zero if an error occurs while skipping whitespace;
otherwise it returns non-zero.
Formatted input functions call ipfx(0)
,
while unformatted input functions call ipfx(1)
;
see below.
>>
xipfx(0)
and if that returns non-zero,
extracts characters from ins and converts them according
to the type of x.
It stores the converted value in x.
Errors are indicated by setting the
error state of ins.
ios::failbit
means that characters
in ins were not a representation of the required type.
ios::badbit
indicates that attempts to extract characters failed.
ins is always returned.
The details of conversion depend on the values of ins's format
state flags and variables (see
ios(C++))
and the type of x.
Except that extractions that use width reset it to 0,
the extraction operators do not change the value
of ostream
's format state.
Extractors are defined for the following types,
with conversion rules as described below.
char*
, unsigned char*
.width()
is non-zero it is taken to be the size of the array, and
no more than ins.width()-1
characters are extracted.
A terminating
null character (0) is always stored (even when nothing else is
done because of ins's error status).
ins.width()
is reset to 0.
char&
, unsigned char&
short&
, unsigned short&
,int&
, unsigned int&
,long&
, unsigned long&
ios::oct
, ios::dec
, or ios::hex
is set in ins.flags()
, the conversion is octal, decimal, or
hexadecimal, respectively.
Conversion is terminated by the first ``non-digit,'' which is left
in ins.
Octal digits are the characters ``0'' to ``7''.
Decimal digits are the octal digits plus ``8'' and ``9''.
Hexadecimal digits
are the decimal digits plus the letters ``a'' through ``f''
(in either upper or lower case).
If none of the conversion base format flags is set,
then the number is interpreted according to C++ lexical conventions.
That is, if the first characters (after the optional
sign) are ``0x'' or ``0X'' a hexadecimal conversion
is performed on following hexadecimal digits.
Otherwise, if the first character is a ``0'',
an octal conversion is performed,
and in all other cases a decimal conversion is performed.
ios::failbit
is set if there are no
digits (not counting the ``0'' in ``0x'' or ``0X'' during
hex conversion) available.
float&
, double&
ios::failbit
is set if there are no
digits available in ins or if it does not begin with a well formed
floating point number.
The type and name (operator>>
) of the extraction operations are
chosen to give a convenient syntax for sequences of input operations.
The operator overloading of C++ permits extraction functions to be
declared for user-defined classes.
These operations can then be used with the same syntax as the member
functions described here.
>>
sbios.ipfx(0)
returns non-zero,
extracts characters from ios
and inserts them into sb.
Extraction stops when EOF is reached.
Always returns ins.
ipfx(1)
and proceed only
if it returns non-zero:
&
ins.get(
ptr,
len,
delim)
get
always stores a terminating null, even if it doesn't extract
any characters from ins because of its error status.
ios::failbit
is set only if get
encounters
an end of file before it stores any characters.
&
ins.get(
c)
&
ins.get(
sb,
delim)
-
Extracts characters from
ins.rdbuf()
and stores
them into sb.
It stops if it encounters end of file or if a store into
sb fails or
if it encounters delim (which it leaves in ins).
ios::failbit
is set if it stops because the store into sb fails.
.get()
.ios::failbit
is never set.
&
ins.getline(
ptr,
len,
delim)
.get(
ptr,
len,
delim)
with the exception
that it extracts a terminating delim character from ins.
In case delim occurs when exactly len characters
have been extracted, termination is treated as being due to the
array being filled, and this delim is left in ins.
&
ins.ignore(
n,
d)
&
ins.read(
ptr,
n)
read
stores whatever it can extract and sets
ios::failbit
.
The number of characters extracted can be determined via ins.gcount()
.
.gcount()
.peek()
.ipfx(1)
.
If that call returns zero or if ins is at end of file,
it returns EOF.
Otherwise it returns the next character without extracting it.
&
ins.putback(
c)
.rdbuf()
.
c must be the character before ins.rdbuf()
's get pointer.
(Unless other activity is modifying ins.rdbuf()
this
is the last character extracted from ins.)
If it is not, the effect is undefined.
putback
may fail (and set the error state).
Although it is a member of istream
,
putback
never extracts characters, so
it does not call ipfx
. It will, however, return without
doing anything if the error state is non-zero.
&
ins.sync()
.rdbuf()->sync()
, which is a virtual function,
so the details depend on the derived class.
Returns EOF to indicate errors.
>>manip
manip(
ins)
.
Syntactically this looks like an extractor
operation, but semantically it does an arbitrary operation
rather than converting a sequence of characters and storing the
result in manip
.
A predefined manipulator, ws, is described below.
&
ins.seekg(
off,
dir)
.rdbuf()
's get pointer.
See
sbuf.pub(C++)
for a discussion of positioning.
&
ins.seekg(
pos)
.rdbuf()
's get pointer.
See
sbuf.pub(C++)
for a discussion of positioning.
.tellg()
ios.rdbuf()
's get pointer.
See
sbuf.pub(C++)
for a discussion of positioning.
>>
ws>>
dec>>
hex>>
oct