📄 istream.html
字号:
<HTML><HEAD><TITLE><istream></TITLE></HEAD><BODY><H1><A NAME="<istream>"><CODE><istream></CODE></A></H1><HR><P>Include the <A HREF="lib_cpp.html#iostreams">iostreams</A>standard header <B><CODE><istream></CODE></B>to define class<CODE><A HREF="#istream">istream</A></CODE>,which mediates extractions for the iostreams.The header also defines a related<A HREF="ios.html#manipulators">manipulator</A>.(This header is typically included for you by anotherof the iostreams headers. You seldom have occasion to include itdirectly.)</P><PRE> // DECLARATIONSclass <B><A HREF="#istream">istream</A></B>; // EXTRACTORSistream& <B><A HREF="#operator>>">operator>></A></B>(istream& istr, char *str);istream& <B><A HREF="#operator>>">operator>></A></B>(istream& istr, char& ch);istream& <B><A HREF="#operator>>">operator>></A></B>(istream& istr, signed char *str);istream& <B><A HREF="#operator>>">operator>></A></B>(istream& istr, signed char& ch);istream& <B><A HREF="#operator>>">operator>></A></B>(istream& istr, unsigned char *str);istream& <B><A HREF="#operator>>">operator>></A></B>(istream& istr, unsigned char& ch); // MANIPULATORSistream& <B><A HREF="#ws">ws</A></B>(istream& istr); // END OF DECLARATIONS</PRE><H2><A NAME="istream"><CODE>istream</CODE></A></H2><HR><P><B><CODE><A HREF="#istream::istream">istream</A>· <A HREF="#istream::gcount">gcount</A>· <A HREF="#istream::get">get</A>· <A HREF="#istream::getline">getline</A>· <A HREF="#istream::ignore">ignore</A>· <A HREF="#istream::operator>>">operator>></A>· <A HREF="#istream::peek">peek</A>· <A HREF="#istream::putback">putback</A>· <A HREF="#istream::read">read</A>· <A HREF="#istream::readsome">readsome</A>· <A HREF="#istream::seekg">seekg</A>· <A HREF="#istream::sentry">sentry</A>· <A HREF="#istream::sync">sync</A>· <A HREF="#istream::tellg">tellg</A>· <A HREF="#istream::unget">unget</A></CODE></B></P><HR><PRE>class <B>istream</B> : public ios {public: explicit <B><A HREF="#istream::istream">istream</A></B>(streambuf *strbuf); class <B><A HREF="#istream::sentry">sentry</A></B>; virtual <B>~istream</B>(); istream& <B><A HREF="#istream::operator>>">operator>></A></B>( istream& (*pfn)(istream&)); istream& <B><A HREF="#istream::operator>>">operator>></A></B>( ios_base& (*pfn)(ios_base&)); istream& <B><A HREF="#istream::operator>>">operator>></A></B>( ios& (*pfn)(ios&)); istream& <B><A HREF="#istream::operator>>">operator>></A></B>( streambuf *strbuf); istream& <B><A HREF="#istream::operator>>">operator>></A></B>(bool& val); istream& <B><A HREF="#istream::operator>>">operator>></A></B>(short& val); istream& <B><A HREF="#istream::operator>>">operator>></A></B>(unsigned short& val); istream& <B><A HREF="#istream::operator>>">operator>></A></B>(int& val); istream& <B><A HREF="#istream::operator>>">operator>></A></B>(unsigned int& val); istream& <B><A HREF="#istream::operator>>">operator>></A></B>(long& val); istream& <B><A HREF="#istream::operator>>">operator>></A></B>(unsigned long& val); istream& <B><A HREF="#istream::operator>>">operator>></A></B>(void *& val); istream& <B><A HREF="#istream::operator>>">operator>></A></B>(float& val); istream& <B><A HREF="#istream::operator>>">operator>></A></B>(double& val); istream& <B><A HREF="#istream::operator>>">operator>></A></B>(long double& val); streamsize <B><A HREF="#istream::gcount">gcount</A></B>() const; int_type <B><A HREF="#istream::get">get</A></B>(); istream& <B><A HREF="#istream::get">get</A></B>(char_type& ch); istream& <B><A HREF="#istream::get">get</A></B>(char_type *str, streamsize count); istream& <B><A HREF="#istream::get">get</A></B>(char_type *str, streamsize count, char_type delim); istream& <B><A HREF="#istream::get">get</A></B>(streambuf& strbuf); istream& <B><A HREF="#istream::get">get</A></B>(streambuf& strbuf, char_type delim); istream& <B><A HREF="#istream::getline">getline</A></B>(char_type *str, streamsize count); istream& <B><A HREF="#istream::getline">getline</A></B>(char_type *str, streamsize count, char_type delim); istream& <B><A HREF="#istream::ignore">ignore</A></B>(streamsize count = 1, int_type delim = traits_type::eof()); int_type <B><A HREF="#istream::peek">peek</A></B>(); istream& <B><A HREF="#istream::read">read</A></B>(char_type *str, streamsize count); streamsize <B><A HREF="#istream::readsome">readsome</A></B>(char_type *str, streamsize count); istream& <B><A HREF="#istream::putback">putback</A></B>(char_type ch); istream& <B><A HREF="#istream::unget">unget</A></B>(); pos_type <B><A HREF="#istream::tellg">tellg</A></B>(); istream& <B><A HREF="#istream::seekg">seekg</A></B>(pos_type pos); istream& <B><A HREF="#istream::seekg">seekg</A></B>(off_type off, ios_base::seek_dir way); int <B><A HREF="#istream::sync">sync</A></B>(); };</PRE><P>The class describes an object that controlsextraction of elements and encoded objects from a<A HREF="streambu.html#stream buffer">stream buffer</A>with elements of type <I>char,</I> also known as<CODE><A HREF="ios.html#ios::char_type">char_type</A></CODE>, whose<A HREF="string2.html#character traits">character traits</A> are determined by theclass <CODE><A HREF="string2.html#char_traits">char_traits</A></CODE>,also known as<CODE><A HREF="ios.html#ios::traits_type">traits_type</A></CODE>.</P><P>Most of the member functions that overload<CODE><A HREF="#istream::operator>>">operator>></A></CODE>are <B><A NAME="formatted input functions">formatted input functions</A></B>.They follow the pattern:</P><PRE> iostate state = goodbit; const sentry ok(*this); if (ok) {try {<extract elements and convert accumulate flags in state store a successful conversion> } catch (...) {try {setstate(badbit); } catch (...) {} if ((exceptions() & badbit) != 0) throw; }} setstate(state); return (*this);</PRE><P>Many other member functions are<B><A NAME="unformatted input functions">unformatted input functions</A></B>.They follow the pattern:</P><PRE> iostate state = goodbit; count = 0; // the value returned by gcount const sentry ok(*this, true); if (ok) {try {<extract elements and deliver count extracted elements in count accumulate flags in state> } catch (...) {try {setstate(badbit); } catch (...) {} if ((exceptions() & badbit) != 0) throw; }} setstate(state);</PRE><P>Both groups of functions call<CODE><A HREF="ios.html#ios::setstate">setstate</A>(eofbit)</CODE>if they encounter end-of-file while extracting elements.</P><P>An object of class <CODE>istream</CODE> stores:</P><UL><LI>a public base object of class<B><CODE><A HREF="ios.html#ios">ios</A></CODE></B></LI><LI>an <B><A NAME="extraction count">extraction count</A></B>for the last unformatted input operation (called <CODE>count</CODE>in the code above)</LI></UL><H3><A NAME="istream::istream"><CODE>istream::istream</CODE></A></H3><PRE>explicit <B>istream</B>(streambuf *strbuf);</PRE><P>The constructor initializes the base class by calling<CODE><A HREF="ios.html#ios::init">init</A>(strbuf)</CODE>.It also stores zero in the<A HREF="#extraction count">extraction count</A>.</P><H3><A NAME="istream::gcount"><CODE>istream::gcount</CODE></A></H3><PRE>streamsize <B>gcount</B>() const;</PRE><P>The member function returns the<A HREF="#extraction count">extraction count</A>.</P><H3><A NAME="istream::get"><CODE>istream::get</CODE></A></H3><PRE>int_type <B><A HREF="#istream::get">get</A></B>();istream& <B>get</B>(char_type& ch);istream& <B>get</B>(char_type *str, streamsize count);istream& <B>get</B>(char_type *str, streamsize count, char_type delim);istream& <B>get</B>(streambuf& strbuf);istream& <B>get</B>(streambuf& strbuf, char_type delim);</PRE><P>The first of these<A HREF="#unformatted input functions">unformatted input functions</A>extracts an element, if possible, as if by returning<CODE>rdbuf()-><A HREF="streambu.html#streambuf::sbumpc">sbumpc</A>()</CODE>.Otherwise, it returns<CODE>traits_type::<A HREF="string2.html#char_traits::eof">eof</A>()</CODE>.If the function extracts no element, it calls<CODE><A HREF="ios.html#ios::setstate">setstate</A>(failbit)</CODE>.</P><P>The second function extracts the<A HREF="ios.html#ios::int_type"><CODE>int_type</CODE></A> element<CODE>meta</CODE> the same way. If <CODE>meta</CODE> compares equal to<CODE>traits_type::<A HREF="string2.html#char_traits::eof">eof</A>()</CODE>,the function calls<CODE><A HREF="ios.html#ios::setstate">setstate</A>(failbit)</CODE>.Otherwise, it stores<CODE>traits_type::<A HREF="string2.html#char_traits::to_char_type">to_char_type</A>(meta)</CODE>in <CODE>ch</CODE>. The function returns <CODE>*this</CODE>.</P><P>The third function returns <CODE>get(str, count, widen('\n'))</CODE>.</P><P>The fourth function extracts up to <CODE>count - 1</CODE> elementsand stores them in the array beginning at <CODE>str</CODE>. It always stores<CODE>char_type()</CODE> afterany extracted elements it stores. In order of testing, extraction stops:</P><OL><LI>at end of file</LI><LI>after the function extracts an element that compares equal to<CODE>delim</CODE>, in which case the element is put backto the controlled sequence</LI><LI>after the function extracts <CODE>count - 1</CODE> elements</LI></OL><P>If the function extracts no elements, it calls<CODE><A HREF="ios.html#ios::setstate">setstate</A>(failbit)</CODE>.In any case, it returns <CODE>*this</CODE>.</P><P>The fifth function returns <CODE>get(strbuf, widen('\n'))</CODE>.</P><P>The sixth function extracts elements and inserts them in<CODE>strbuf</CODE>. Extraction stops on end-of-file or on an elementthat compares equal to <CODE>delim</CODE> (which is not extracted).It also stops, without extracting the element in question,if an insertion fails or throws an exception (which is caughtbut not rethrown). If the function extracts no elements, it calls<CODE><A HREF="ios.html#ios::setstate">setstate</A>(failbit)</CODE>.In any case, the function returns <CODE>*this</CODE>.</P><H3><A NAME="istream::getline"><CODE>istream::getline</CODE></A></H3><PRE>istream& <B>getline</B>(char_type *str, streamsize count);istream& <B>getline</B>(char_type *str, streamsize count, char_type delim);</PRE><P>The first of these<A HREF="#unformatted input functions">unformatted input functions</A>returns <CODE>getline(str, count, widen('\n'))</CODE>.</P><P>The second function extracts up to <CODE>count - 1</CODE> elementsand stores them in the array beginning at <CODE>str</CODE>. It always stores<CODE>char_type()</CODE> afterany extracted elements it stores. In order of testing, extraction stops:</P><OL><LI>at end of file</LI><LI>after the function extracts an element that compares equal to<CODE>delim</CODE>, in which case the element is neither put back norappended to the controlled sequence</LI><LI>after the function extracts <CODE>count - 1</CODE> elements</LI></OL><P>If the function extracts no elements or <CODE>count - 1</CODE> elements, it calls<CODE><A HREF="ios.html#ios::setstate">setstate</A>(failbit)</CODE>.In any case, it returns <CODE>*this</CODE>.</P><H3><A NAME="istream::ignore"><CODE>istream::ignore</CODE></A></H3><PRE>istream& <B>ignore</B>(streamsize count = 1, int_type delim = traits_type::eof());</PRE><P>The <A HREF="#unformatted input functions">unformatted input function</A>extracts up to <CODE>count</CODE> elements and discards them.If <CODE>count</CODE> equals<CODE><A HREF="limits.html#INT_MAX">INT_MAX</A></CODE>,however, it is taken as arbitrarily large.Extraction stops early on end-of-file oron an element <CODE>ch</CODE> such that<CODE>traits_type::<A HREF="string2.html#char_traits::to_int_type">to_int_type</A>(ch)</CODE>compares equal to <CODE>delim</CODE> (which is also extracted).The function returns <CODE>*this</CODE>.</P><H3><A NAME="istream::operator>>"><CODE>istream::operator>></CODE></A></H3><PRE>istream& <B>operator>></B>( istream& (*pfn)(istream&));istream& <B>operator>></B>( ios_base& (*pfn)(ios_base&));istream& <B>operator>></B>( ios& (*pfn)(ios&));istream& <B>operator>></B>( streambuf *strbuf);istream& <B>operator>></B>(bool& val);istream& <B>operator>></B>(short& val);istream& <B>operator>></B>(unsigned short& val);istream& <B>operator>></B>(int& val);istream& <B>operator>></B>(unsigned int& val);istream& <B>operator>></B>(long& val);istream& <B>operator>></B>(unsigned long& val);istream& <B>operator>></B>(void *& val);istream& <B>operator>></B>(float& val);istream& <B>operator>></B>(double& val);istream& <B>operator>></B>(long double& val);</PRE><P>The first member function ensures that an expression of theform <CODE>istr >> ws</CODE> calls<CODE><A HREF="#ws">ws</A>(istr)</CODE>, then returns <CODE>*this</CODE>.The second and third functions ensure that other<A HREF="ios.html#manipulators">manipulators</A>,such as <CODE><A HREF="ios.html#hex">hex</A></CODE> behavesimilarly. The remaining functions constitute the<A HREF="#formatted input functions">formatted input functions</A>.</P><P>The function:</P><PRE>istream& <B>operator>></B>( streambuf *strbuf);</PRE><P>extracts elements, if <CODE>strbuf</CODE> is not a null pointer,and inserts them in <CODE>strbuf</CODE>. Extraction stops on end-of-file.It also stops, without extracting the element in question,if an insertion fails or throws an exception (which is caughtbut not rethrown).If the function extracts no elements, it calls<CODE><A HREF="ios.html#ios::setstate">setstate</A>(failbit)</CODE>.In any case, the function returns <CODE>*this</CODE>.</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -