📄 iostream.html
字号:
<DD><A NAME="IDX67"></A>Return a reference to an arbitrary pointer, stored in an <CODE>ios</CODE>instance. <VAR>index</VAR>, originally returned from <CODE>ios::xalloc</CODE>,identifies what particular pointer you need.</DL></P><P><DL><DT><U>Method:</U> void* <B>ios::pword</B> <I>(int <VAR>index</VAR>) const</I><DD><A NAME="IDX68"></A>Return the actual value of a pointer stored in an <CODE>ios</CODE>.</DL></P><H3><A NAME="SEC11" HREF="iostream_toc.html#TOC11">Synchronizing related streams</A></H3><P>You can use these methods to synchronize related streams withone another:</P><P><DL><DT><U>Method:</U> ostream* <B>ios::tie</B> <I>() const</I><DD><A NAME="IDX69"></A>Report on what output stream, if any, is to be flushed before accessingthis one. A pointer value of <CODE>0</CODE> means no stream is tied.</DL></P><P><DL><DT><U>Method:</U> ostream* <B>ios::tie</B> <I>(ostream* <VAR>assoc</VAR>)</I><DD><A NAME="IDX70"></A>Declare that output stream <VAR>assoc</VAR> must be flushed before accessingthis stream.</DL></P><P><DL><DT><U>Method:</U> int <B>ios::sync_with_stdio</B> <I>([int <VAR>switch</VAR>])</I><DD><A NAME="IDX71"></A>Unless iostreams and C <CODE>stdio</CODE> are designed to work together, youmay have to choose between efficient C++ streams output and outputcompatible with C <CODE>stdio</CODE>. Use <SAMP>`ios::sync_with_stdio()'</SAMP> toselect C compatibility.</P><P>The argument <VAR>switch</VAR> is a GNU extension; use <CODE>0</CODE> as theargument to choose output that is not necessarily compatible with C<CODE>stdio</CODE>. The default value for <VAR>switch</VAR> is <CODE>1</CODE>.</P><P>If you install the <CODE>stdio</CODE> implementation that comes with GNU<CODE>libio</CODE>, there are compatible input/output facilities for both Cand C++. In that situation, this method is unnecessary--but you maystill want to write programs that call it, for portability.</DL></P><H3><A NAME="SEC12" HREF="iostream_toc.html#TOC12">Reaching the underlying <CODE>streambuf</CODE></A></H3><P>Finally, you can use this method to access the underlying object:</P><P><DL><DT><U>Method:</U> streambuf* <B>ios::rdbuf</B> <I>() const</I><DD><A NAME="IDX72"></A>Return a pointer to the <CODE>streambuf</CODE> object that underlies this<CODE>ios</CODE>.</DL></P><H2><A NAME="SEC13" HREF="iostream_toc.html#TOC13">Managing output streams: class <CODE>ostream</CODE></A></H2><P>Objects of class <CODE>ostream</CODE> inherit the generic methods from<CODE>ios</CODE>, and in addition have the following methods available.Declarations for this class come from <TT>`iostream.h'</TT>.</P><P><DL><DT><U>Constructor:</U> <B>ostream::ostream</B> <I>()</I><DD><A NAME="IDX73"></A>The simplest form of the constructor for an <CODE>ostream</CODE> simplyallocates a new <CODE>ios</CODE> object.</DL></P><P><DL><DT><U>Constructor:</U> <B>ostream::ostream</B> <I>(streambuf* <VAR>sb</VAR> [, ostream <VAR>tie</VAR>])</I><DD><A NAME="IDX74"></A>This alternative constructor requires a first argument <VAR>sb</VAR> of type<CODE>streambuf*</CODE>, to use an existing open stream for output. It alsoaccepts an optional second argument <VAR>tie</VAR>, to specify a related<CODE>ostream*</CODE> as the initial value for <CODE>ios::tie</CODE>.</P><P>If you give the <CODE>ostream</CODE> a <CODE>streambuf</CODE> explicitly, usingthis constructor, the <VAR>sb</VAR> is <EM>not</EM> destroyed (or deleted orclosed) when the <CODE>ostream</CODE> is destroyed.</DL></P><H3><A NAME="SEC14" HREF="iostream_toc.html#TOC14">Writing on an <CODE>ostream</CODE></A></H3><P>These methods write on an <CODE>ostream</CODE> (you may also use the operator<CODE><<</CODE>; see section <A HREF="iostream.html#SEC4">Operators and Default Streams</A>).</P><P><DL><DT><U>Method:</U> ostream& <B>ostream::put</B> <I>(char <VAR>c</VAR>)</I><DD><A NAME="IDX75"></A>Write the single character <VAR>c</VAR>.</DL></P><P><DL><DT><U>Method:</U> ostream& <B>ostream::write</B> <I>(<VAR>string</VAR>, int <VAR>length</VAR>)</I><DD><A NAME="IDX76"></A>Write <VAR>length</VAR> characters of a string to this <CODE>ostream</CODE>,beginning at the pointer <VAR>string</VAR>.</P><P><VAR>string</VAR> may have any of these types: <CODE>char*</CODE>, <CODE>unsignedchar*</CODE>, <CODE>signed char*</CODE>.</DL></P><P><DL><DT><U>Method:</U> ostream& <B>ostream::form</B> <I>(const char *<VAR>format</VAR>, ...)</I><DD><A NAME="IDX77"></A>A GNU extension, similar to <CODE>fprintf(<VAR>file</VAR>,<VAR>format</VAR>, ...)</CODE>.</P><P><VAR>format</VAR> is a <CODE>printf</CODE>-style format control string, which is usedto format the (variable number of) arguments, printing the result onthis <CODE>ostream</CODE>. See <CODE>ostream::vform</CODE> for a version that usesan argument list rather than a variable number of arguments.</DL></P><P><DL><DT><U>Method:</U> ostream& <B>ostream::vform</B> <I>(const char *<VAR>format</VAR>, va_list <VAR>args</VAR>)</I><DD><A NAME="IDX78"></A>A GNU extension, similar to <CODE>vfprintf(<VAR>file</VAR>,<VAR>format</VAR>, <VAR>args</VAR>)</CODE>.</P><P><VAR>format</VAR> is a <CODE>printf</CODE>-style format control string, which is usedto format the argument list <VAR>args</VAR>, printing the result onthis <CODE>ostream</CODE>. See <CODE>ostream::form</CODE> for a version that uses avariable number of arguments rather than an argument list.</DL></P><H3><A NAME="SEC15" HREF="iostream_toc.html#TOC15">Repositioning an <CODE>ostream</CODE></A></H3><P>You can control the output position (on output streams that actuallysupport positions, typically files) with these methods:</P><P><DL><DT><U>Method:</U> streampos <B>ostream::tellp</B> <I>()</I><DD><A NAME="IDX79"></A>Return the current write position in the stream.</DL></P><P><DL><DT><U>Method:</U> ostream& <B>ostream::seekp</B> <I>(streampos <VAR>loc</VAR>)</I><DD><A NAME="IDX80"></A>Reset the output position to <VAR>loc</VAR> (which is usually the result of aprevious call to <CODE>ostream::tellp</CODE>). <VAR>loc</VAR> specifies anabsolute position in the output stream.</DL></P><P><DL><DT><U>Method:</U> ostream& <B>ostream::seekp</B> <I>(streamoff <VAR>loc</VAR>, <VAR>rel</VAR>)</I><DD><A NAME="IDX81"></A><A NAME="IDX82"></A>Reset the output position to <VAR>loc</VAR>, relative to the beginning, end,or current output position in the stream, as indicated by <VAR>rel</VAR> (avalue from the enumeration <CODE>ios::seekdir</CODE>):</P><DL COMPACT><DT><CODE>beg</CODE><DD><A NAME="IDX83"></A>Interpret <VAR>loc</VAR> as an absolute offset from the beginning of thefile.<DT><CODE>cur</CODE><DD><A NAME="IDX84"></A>Interpret <VAR>loc</VAR> as an offset relative to the current outputposition.<DT><CODE>end</CODE><DD><A NAME="IDX85"></A>Interpret <VAR>loc</VAR> as an offset from the current end of the outputstream.</DL></DL><H3><A NAME="SEC16" HREF="iostream_toc.html#TOC16">Miscellaneous <CODE>ostream</CODE> utilities</A></H3><P>You may need to use these <CODE>ostream</CODE> methods for housekeeping:</P><P><DL><DT><U>Method:</U> ostream& <B>flush</B> <I>()</I><DD><A NAME="IDX86"></A>Deliver any pending buffered output for this <CODE>ostream</CODE>.</DL></P><P><DL><DT><U>Method:</U> int <B>ostream::opfx</B> <I>()</I><DD><A NAME="IDX87"></A><CODE>opfx</CODE> is a <EM>prefix</EM> method for operations on <CODE>ostream</CODE>objects; it is designed to be called before any further processing. See<CODE>ostream::osfx</CODE> for the converse.</P><P><CODE>opfx</CODE> tests that the stream is in state <CODE>good</CODE>, and if soflushes any stream tied to this one.</P><P>The result is <CODE>1</CODE> when <CODE>opfx</CODE> succeeds; else (if the stream state isnot <CODE>good</CODE>), the result is <CODE>0</CODE>.</DL></P><P><DL><DT><U>Method:</U> void <B>ostream::osfx</B> <I>()</I><DD><A NAME="IDX88"></A><CODE>osfx</CODE> is a <EM>suffix</EM> method for operations on <CODE>ostream</CODE>objects; it is designed to be called at the conclusion of any processing. Allthe <CODE>ostream</CODE> methods end by calling <CODE>osfx</CODE>. See<CODE>ostream::opfx</CODE> for the converse.</P><P>If the <CODE>unitbuf</CODE> flag is set for this stream, <CODE>osfx</CODE> flushesany buffered output for it.</P><P>If the <CODE>stdio</CODE> flag is set for this stream, <CODE>osfx</CODE> flushes anyoutput buffered for the C output streams <TT>`stdout'</TT> and <TT>`stderr'</TT>.</DL></P><H2><A NAME="SEC17" HREF="iostream_toc.html#TOC17">Managing input streams: class <CODE>istream</CODE></A></H2><P>Class <CODE>istream</CODE> objects are specialized for input; as for<CODE>ostream</CODE>, they are derived from <CODE>ios</CODE>, so you can use any ofthe general-purpose methods from that base class. Declarations for thisclass also come from <TT>`iostream.h'</TT>.</P><P><DL><DT><U>Constructor:</U> <B>istream::istream</B> <I>()</I><DD><A NAME="IDX89"></A>When used without arguments, the <CODE>istream</CODE> constructor simplyallocates a new <CODE>ios</CODE> object and initializes the input counter (thevalue reported by <CODE>istream::gcount</CODE>) to <CODE>0</CODE>.</DL></P><P><DL><DT><U>Constructor:</U> <B>istream::istream</B> <I>(streambuf *<VAR>sb</VAR> [, ostream <VAR>tie</VAR>])</I><DD><A NAME="IDX90"></A>You can also call the constructor with one or two arguments. The firstargument <VAR>sb</VAR> is a <CODE>streambuf*</CODE>; if you supply this pointer,the constructor uses that <CODE>streambuf</CODE> for input.You can use the second optional argument <VAR>tie</VAR> to specify a relatedoutput stream as the initial value for <CODE>ios::tie</CODE>.</P><P>If you give the <CODE>istream</CODE> a <CODE>streambuf</CODE> explicitly, usingthis constructor, the <VAR>sb</VAR> is <EM>not</EM> destroyed (or deleted orclosed) when the <CODE>ostream</CODE> is destroyed.</DL></P><H3><A NAME="SEC18" HREF="iostream_toc.html#TOC18">Reading one character</A></H3><P>Use these methods to read a single character from the input stream:</P><P><DL><DT><U>Method:</U> int <B>istream::get</B> <I>()</I><DD><A NAME="IDX91"></A>Read a single character (or <CODE>EOF</CODE>) from the input stream, returningit (coerced to an unsigned char) as the result.</DL></P><P><DL><DT><U>Method:</U> istream& <B>istream::get</B> <I>(char& <VAR>c</VAR>)</I><DD><A NAME="IDX92"></A>Read a single character from the input stream, into <CODE>&<VAR>c</VAR></CODE>.</DL></P><P><DL><DT><U>Method:</U> int <B>istream::peek</B> <I>()</I><DD><A NAME="IDX93"></A>Return the next available input character, but <EM>without</EM> changingthe current input position.</DL></P><H3><A NAME="SEC19" HREF="iostream_toc.html#TOC19">Reading strings</A></H3><P>Use these methods to read strings (for example, a line at a time) fromthe input stream:</P><P><DL><DT><U>Method:</U> istream& <B>istream::get</B> <I>(char* <VAR>c</VAR>, int <VAR>len</VAR> [, char <VAR>delim</VAR>])</I><DD><A NAME="IDX94"></A>Read a string from the input stream, into the array at <VAR>c</VAR>.</P><P>The remaining arguments limit how much to read: up to <SAMP>`len-1'</SAMP>characters, or up to (but not including) the first occurrence in theinput of a particular delimiter character <VAR>delim</VAR>---newline(<CODE>\n</CODE>) by default. (Naturally, if the stream reaches end of filefirst, that too will terminate reading.)</P><P>If <VAR>delim</VAR> was present in the input, it remains available as ifunread; to discard it instead, see <CODE>iostream::getline</CODE>.</P><P><CODE>get</CODE> writes <SAMP>`\0'</SAMP> at the end of the string, regardlessof which condition terminates the read.</DL></P><P><DL><DT><U>Method:</U> istream& <B>istream::get</B> <I>(streambuf& <VAR>sb</VAR> [, char <VAR>delim</VAR>])</I><DD><A NAME="IDX95"></A>Read characters from the input stream and copy them on the<CODE>streambuf</CODE> object <VAR>sb</VAR>. Copying ends either just before thenext instance of the delimiter character <VAR>delim</VAR> (newline <CODE>\n</CODE>by default), or when either stream ends. If <VAR>delim</VAR> was present inthe input, it remains available as if unread.</DL></P><P><DL><DT><U>Method:</U> istream& <B>istream::getline</B> <I>(<VAR>charptr</VAR>, int <VAR>len</VAR> [, char <VAR>delim</VAR>])</I><DD><A NAME="IDX96"></A>Read a line from the input stream, into the array at <VAR>charptr</VAR>.<VAR>charptr</VAR> may be any of three kinds of pointer: <CODE>char*</CODE>,<CODE>unsigned char*</CODE>, or <CODE>signed char*</CODE>.</P><P>The remaining arguments limit how much to read: up to (but notincluding) the first occurrence in the input of a line delimitercharacter <VAR>delim</VAR>---newline (<CODE>\n</CODE>) by default, or up to<SAMP>`len-1'</SAMP> characters (or to end of file, if that happens sooner).</P><P>If <CODE>getline</CODE> succeeds in reading a "full line", it also discardsthe trailing delimiter character from the input stream. (To preserve itas available input, see the similar form of <CODE>iostream::get</CODE>.)</P><P>If <VAR>delim</VAR> was <EM>not</EM> found before <VAR>len</VAR> characters or endof file, <CODE>getline</CODE> sets the <CODE>ios::fail</CODE> flag, as well as the<CODE>ios::eof</CODE> flag if appropriate.</P><P><CODE>getline</CODE> writes a null character at the end of the string, regardlessof which condition terminates the read.</DL></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -