istream
Class istream
objects are specialized for input; as for
ostream
, they are derived from ios
, so you can use any of
the general-purpose methods from that base class. Declarations for this
class also come from iostream.h.
istream
constructor simply
allocates a new ios
object and initializes the input counter (the
value reported by istream::gcount
) to 0
.
streambuf*
; if you supply this pointer,
the constructor uses that streambuf
for input.
You can use the second optional argument tie to specify a related
output stream as the initial value for ios::tie
.
If you give the istream
a streambuf
explicitly, using
this constructor, the sb is not destroyed (or deleted or
closed) when the ostream
is destroyed.
Use these methods to read a single character from the input stream:
EOF
) from the input stream, returning
it (coerced to an unsigned char) as the result.
&c
.
Use these methods to read strings (for example, a line at a time) from the input stream:
The remaining arguments limit how much to read: up to `len-1
'
characters, or up to (but not including) the first occurrence in the
input of a particular delimiter character delim---newline
(\n
) by default. (Naturally, if the stream reaches end of file
first, that too will terminate reading.)
If delim was present in the input, it remains available as if
unread; to discard it instead, see iostream::getline
.
get
writes `\0
' at the end of the string, regardless
of which condition terminates the read.
streambuf
object sb. Copying ends either just before the
next instance of the delimiter character delim (newline \n
by default), or when either stream ends. If delim was present in
the input, it remains available as if unread.
char*
,
unsigned char*
, or signed char*
.
The remaining arguments limit how much to read: up to (but not
including) the first occurrence in the input of a line delimiter
character delim---newline (\n
) by default, or up to
`len-1
' characters (or to end of file, if that happens sooner).
If getline
succeeds in reading a ``full line'', it also discards
the trailing delimiter character from the input stream. (To preserve it
as available input, see the similar form of iostream::get
.)
If delim was not found before len characters or end
of file, getline
sets the ios::fail
flag, as well as the
ios::eof
flag if appropriate.
getline
writes a null character at the end of the string, regardless
of which condition terminates the read.
pointer may be of type char*
, void*
, unsigned char*
, or signed char*
.
If the istream
ends before reading len bytes, read
sets the ios::fail
flag.
\n
by default).
To permit reading a string of arbitrary length, gets
allocates
whatever memory is required. Notice that the first argument s is
an address to record a character pointer, rather than the pointer
itself.
fscanf(file, format, ...)
. The format is a scanf
-style format
control string, which is used to read the variables in the remainder of
the argument list from the istream
.
istream::scan
, but takes a single va_list
argument.
istream
Use these methods to control the current input position:
istream::seekg
.
istream::tellg
.
ios::beg
ios::cur
ios::end
istream
utilities
Use these methods for housekeeping on istream
objects:
istream
in the
last unformatted input operation.
istream
object is ready for reading; check for
errors and end of file and flush any tied stream. ipfx
skips
whitespace if you specify 0
as the keepwhite
argument, and ios::skipws
is set for this stream.
To avoid skipping whitespace (regardless of the skipws
setting on
the stream), use 1
as the argument.
Call istream::ipfx
to simplify writing your own methods for reading
istream
objects.
If you wish to write portable standard-conforming code on istream
objects, call isfx
after any operation that reads from an
istream
; if istream::ipfx
has any special effects that
must be cancelled when done, istream::isfx
will cancel them.
ignore
returns immediately if this character appears in the
input.
By default, delim is EOF
; that is, if you do not specify a
second argument, only the count n restricts how much to ignore
(while input is still available).
If you do not specify how many characters to ignore, ignore
returns after discarding only one character.
EOF
if this is not allowed. Putting
back the most recently read character is always allowed. (This method
corresponds to the C function ungetc
.)