📄 iostream.html
字号:
</P><P><DL><DT><U>Method:</U> int <B>ios::fail</B> <I>() const</I><DD><A NAME="IDX20"></A>Test for any kind of failure on this stream: <EM>either</EM> someoperation failed, <EM>or</EM> the stream is marked as bad. (If either<CODE>ios::failbit</CODE> or <CODE>ios::badbit</CODE> is set.)</DL></P><P><DL><DT><U>Method:</U> void <B>ios::clear</B> <I>(iostate <VAR>state</VAR>)</I><DD><A NAME="IDX21"></A>Set the state indication for this stream to the argument <VAR>state</VAR>.You may call <CODE>ios::clear</CODE> with no argument, in which case the stateis set to <CODE>good</CODE> (no errors pending).</P><P>See <CODE>ios::good</CODE>, <CODE>ios::eof</CODE>, <CODE>ios::fail</CODE>, and<CODE>ios::bad</CODE>, to test the state; see <CODE>ios::set</CODE> or<CODE>ios::setstate</CODE> for an alternative way of setting the state.</DL></P><H3><A NAME="SEC8" HREF="iostream_toc.html#TOC8">Choices in formatting</A></H3><P>These methods control (or report on) settings for some details ofcontrolling streams, primarily to do with formatting output:</P><P><DL><DT><U>Method:</U> char <B>ios::fill</B> <I>() const</I><DD><A NAME="IDX22"></A>Report on the padding character in use.</DL></P><P><DL><DT><U>Method:</U> char <B>ios::fill</B> <I>(char <VAR>padding</VAR>)</I><DD><A NAME="IDX23"></A>Set the padding character. You can also use the manipulator<CODE>setfill</CODE>. See section <A HREF="iostream.html#SEC9">Changing stream properties using manipulators</A>.</P><P>Default: blank.</DL></P><P><DL><DT><U>Method:</U> int <B>ios::precision</B> <I>() const</I><DD><A NAME="IDX24"></A>Report the number of significant digits currently in use for output offloating point numbers.</P><P>Default: <CODE>6</CODE>.</DL></P><P><DL><DT><U>Method:</U> int <B>ios::precision</B> <I>(int <VAR>signif</VAR>)</I><DD><A NAME="IDX25"></A>Set the number of significant digits (for input and output numericconversions) to <VAR>signif</VAR>.</P><P><A NAME="IDX26"></A><A NAME="IDX27"></A>You can also use the manipulator <CODE>setprecision</CODE> for this purpose.See section <A HREF="iostream.html#SEC9">Changing stream properties using manipulators</A>.</DL></P><P><DL><DT><U>Method:</U> int <B>ios::width</B> <I>() const</I><DD><A NAME="IDX28"></A>Report the current output field width setting (the number ofcharacters to write on the next <SAMP>`<<'</SAMP> output operation).</P><P>Default: <CODE>0</CODE>, which means to use as many characters as necessary.</DL></P><P><DL><DT><U>Method:</U> int <B>ios::width</B> <I>(int <VAR>num</VAR>)</I><DD><A NAME="IDX29"></A>Set the input field width setting to <VAR>num</VAR>. Return the<EM>previous</EM> value for this stream.</P><P><A NAME="IDX30"></A><A NAME="IDX31"></A>This value resets to zero (the default) every time you use <SAMP>`<<'</SAMP>; it isessentially an additional implicit argument to that operator. You canalso use the manipulator <CODE>setw</CODE> for this purpose.See section <A HREF="iostream.html#SEC9">Changing stream properties using manipulators</A>.</DL></P><P><DL><DT><U>Method:</U> fmtflags <B>ios::flags</B> <I>() const</I><DD><A NAME="IDX32"></A>Return the current value of the complete collection of flags controllingthe format state. These are the flags and their meanings when set:</P><DL COMPACT><DT><CODE>ios::dec</CODE><DD><A NAME="IDX33"></A><DT><CODE>ios::oct</CODE><DD><A NAME="IDX34"></A><DT><CODE>ios::hex</CODE><DD><A NAME="IDX35"></A>What numeric base to use in converting integers from internal to displayrepresentation, or vice versa: decimal, octal, or hexadecimal,respectively. (You can change the base using the manipulator<CODE>setbase</CODE>, or any of the manipulators <CODE>dec</CODE>, <CODE>oct</CODE>, or<CODE>hex</CODE>; see section <A HREF="iostream.html#SEC9">Changing stream properties using manipulators</A>.)On input, if none of these flags is set, read numeric constantsaccording to the prefix: decimal if no prefix (or a <SAMP>`.'</SAMP> suffix),octal if a <SAMP>`0'</SAMP> prefix is present, hexadecimal if a <SAMP>`0x'</SAMP> prefixis present.Default: <CODE>dec</CODE>.<DT><CODE>ios::fixed</CODE><DD><A NAME="IDX36"></A>Avoid scientific notation, and always show a fixed number of digits afterthe decimal point, according to the output precision in effect.Use <CODE>ios::precision</CODE> to set precision.<DT><CODE>ios::left</CODE><DD><A NAME="IDX37"></A><DT><CODE>ios::right</CODE><DD><A NAME="IDX38"></A><DT><CODE>ios::internal</CODE><DD><A NAME="IDX39"></A>Where output is to appear in a fixed-width field; left-justified,right-justified, or with padding in the middle (e.g. between a numericsign and the associated value), respectively.<DT><CODE>ios::scientific</CODE><DD><A NAME="IDX40"></A>Use scientific (exponential) notation to display numbers.<DT><CODE>ios::showbase</CODE><DD><A NAME="IDX41"></A>Display the conventional prefix as a visual indicator of the conversionbase: no prefix for decimal, <SAMP>`0'</SAMP> for octal, <SAMP>`0x'</SAMP> for hexadecimal.<DT><CODE>ios::showpoint</CODE><DD><A NAME="IDX42"></A>Display a decimal point and trailing zeros after it to fill out numericfields, even when redundant.<DT><CODE>ios::showpos</CODE><DD><A NAME="IDX43"></A>Display a positive sign on display of positive numbers.<DT><CODE>ios::skipws</CODE><DD><A NAME="IDX44"></A>Skip white space. (On by default).<DT><CODE>ios::stdio</CODE><DD><A NAME="IDX45"></A>Flush the C <CODE>stdio</CODE> streams <CODE>stdout</CODE> and <CODE>stderr</CODE> aftereach output operation (for programs that mix C and C++ output conventions).<DT><CODE>ios::unitbuf</CODE><DD><A NAME="IDX46"></A>Flush after each output operation.<DT><CODE>ios::uppercase</CODE><DD><A NAME="IDX47"></A>Use upper-case characters for the non-numeral elements in numericdisplays; for instance, <SAMP>`0X7A'</SAMP> rather than <SAMP>`0x7a'</SAMP>, or<SAMP>`3.14E+09'</SAMP> rather than <SAMP>`3.14e+09'</SAMP>.</DL></DL><P><DL><DT><U>Method:</U> fmtflags <B>ios::flags</B> <I>(fmtflags <VAR>value</VAR>)</I><DD><A NAME="IDX48"></A>Set <VAR>value</VAR> as the complete collection of flags controlling theformat state. The flag values are described under <SAMP>`ios::flags ()'</SAMP>.</P><P>Use <CODE>ios::setf</CODE> or <CODE>ios::unsetf</CODE> to change one property at atime.</DL></P><P><DL><DT><U>Method:</U> fmtflags <B>ios::setf</B> <I>(fmtflags <VAR>flag</VAR>)</I><DD><A NAME="IDX49"></A>Set one particular flag (of those described for <SAMP>`ios::flags ()'</SAMP>;return the complete collection of flags <EM>previously</EM> in effect.(Use <CODE>ios::unsetf</CODE> to cancel.)</DL></P><P><DL><DT><U>Method:</U> fmtflags <B>ios::setf</B> <I>(fmtflags <VAR>flag</VAR>, fmtflags <VAR>mask</VAR>)</I><DD><A NAME="IDX50"></A>Clear the flag values indicated by <VAR>mask</VAR>, then set any of them thatare also in <VAR>flag</VAR>. (Flag values are described for <SAMP>`ios::flags()'</SAMP>.) Return the complete collection of flags <EM>previously</EM> ineffect. (See <CODE>ios::unsetf</CODE> for another way of clearing flags.)</DL></P><P><DL><DT><U>Method:</U> fmtflags <B>ios::unsetf</B> <I>(fmtflags <VAR>flag</VAR>)</I><DD><A NAME="IDX51"></A>Make certain <VAR>flag</VAR> (a combination of flag values described for<SAMP>`ios::flags ()'</SAMP>) is not set for this stream; converse of<CODE>ios::setf</CODE>. Returns the old values of those flags.</DL></P><H3><A NAME="SEC9" HREF="iostream_toc.html#TOC9">Changing stream properties using manipulators</A></H3><P>For convenience, <VAR>manipulators</VAR> provide a way to change certainproperties of streams, or otherwise affect them, in the middle ofexpressions involving <SAMP>`<<'</SAMP> or <SAMP>`>>'</SAMP>. For example, you mightwrite</P><PRE>cout << "|" << setfill('*') << setw(5) << 234 << "|";</PRE><P>to produce <SAMP>`|**234|'</SAMP> as output.</P><P><DL><DT><U>Manipulator:</U> <B>ws</B><DD><A NAME="IDX52"></A>Skip whitespace.</DL></P><P><DL><DT><U>Manipulator:</U> <B>flush</B><DD><A NAME="IDX53"></A>Flush an output stream. For example, <SAMP>`cout << ... <<flush;'</SAMP>has the same effect as <SAMP>`cout << ...; cout.flush();'</SAMP>.</DL></P><P><DL><DT><U>Manipulator:</U> <B>endl</B><DD><A NAME="IDX54"></A>Write an end of line character <SAMP>`\n'</SAMP>, then flushes the output stream.</DL></P><P><DL><DT><U>Manipulator:</U> <B>ends</B><DD><A NAME="IDX55"></A>Write <SAMP>`\0'</SAMP> (the string terminator character).</DL></P><P><DL><DT><U>Manipulator:</U> <B>setprecision</B> <I>(int <VAR>signif</VAR>)</I><DD><A NAME="IDX56"></A>You can change the value of <CODE>ios::precision</CODE> in <SAMP>`<<'</SAMP>expressions with the manipulator <SAMP>`setprecision(<VAR>signif</VAR>)'</SAMP>; forexample,</P><PRE>cout << setprecision(2) << 4.567;</PRE><P>prints <SAMP>`4.6'</SAMP>. Requires <TT>`#include <iomanip.h>'</TT>.</DL></P><P><DL><DT><U>Manipulator:</U> <B>setw</B> <I>(int <VAR>n</VAR>)</I><DD><A NAME="IDX57"></A>You can change the value of <CODE>ios::width</CODE> in <SAMP>`<<'</SAMP> expressionswith the manipulator <SAMP>`setw(<VAR>n</VAR>)'</SAMP>; for example,</P><PRE>cout << setw(5) << 234;</PRE><P>prints <SAMP>` 234'</SAMP> with two leading blanks. Requires <TT>`#include<iomanip.h>'</TT>.</DL></P><P><DL><DT><U>Manipulator:</U> <B>setbase</B> <I>(int <VAR>base</VAR>)</I><DD><A NAME="IDX58"></A>Where <VAR>base</VAR> is one of <CODE>10</CODE> (decimal), <CODE>8</CODE> (octal), or<CODE>16</CODE> (hexadecimal), change the base value for numericrepresentations. Requires <TT>`#include <iomanip.h>'</TT>.</DL></P><P><DL><DT><U>Manipulator:</U> <B>dec</B><DD><A NAME="IDX59"></A>Select decimal base; equivalent to <SAMP>`setbase(10)'</SAMP>.</DL></P><P><DL><DT><U>Manipulator:</U> <B>hex</B><DD><A NAME="IDX60"></A>Select hexadecimal base; equivalent to <SAMP>`setbase(16)'</SAMP>.</DL></P><P><DL><DT><U>Manipulator:</U> <B>oct</B><DD><A NAME="IDX61"></A>Select octal base; equivalent to <SAMP>`setbase(8)'</SAMP>.</DL></P><P><DL><DT><U>Manipulator:</U> <B>setfill</B> <I>(char <VAR>padding</VAR>)</I><DD><A NAME="IDX62"></A>Set the padding character, in the same way as <CODE>ios::fill</CODE>.Requires <TT>`#include <iomanip.h>'</TT>.</DL></P><H3><A NAME="SEC10" HREF="iostream_toc.html#TOC10">Extended data fields</A></H3><P>A related collection of methods allows you to extend this collection offlags and parameters for your own applications, without risk of conflictbetween them:</P><P><DL><DT><U>Method:</U> static fmtflags <B>ios::bitalloc</B> <I>()</I><DD><A NAME="IDX63"></A>Reserve a bit (the single bit on in the result) to use as a flag. Using<CODE>bitalloc</CODE> guards against conflict between two packages that use<CODE>ios</CODE> objects for different purposes.</P><P>This method is available for upward compatibility, but is not in theANSI working paper. The number of bits available is limited; areturn value of <CODE>0</CODE> means no bit is available.</DL></P><P><DL><DT><U>Method:</U> static int <B>ios::xalloc</B> <I>()</I><DD><A NAME="IDX64"></A>Reserve space for a long integer or pointer parameter. The result is aunique nonnegative integer. You can use it as an index to<CODE>ios::iword</CODE> or <CODE>ios::pword</CODE>. Use <CODE>xalloc</CODE> to arrangefor arbitrary special-purpose data in your <CODE>ios</CODE> objects, withoutrisk of conflict between packages designed for different purposes.</DL></P><P><DL><DT><U>Method:</U> long& <B>ios::iword</B> <I>(int <VAR>index</VAR>)</I><DD><A NAME="IDX65"></A>Return a reference to arbitrary data, of long integer type, stored in an<CODE>ios</CODE> instance. <VAR>index</VAR>, conventionally returned from<CODE>ios::xalloc</CODE>, identifies what particular data you need.</DL></P><P><DL><DT><U>Method:</U> long <B>ios::iword</B> <I>(int <VAR>index</VAR>) const</I><DD><A NAME="IDX66"></A>Return the actual value of a long integer stored in an <CODE>ios</CODE>.</DL></P><P><DL><DT><U>Method:</U> void*& <B>ios::pword</B> <I>(int <VAR>index</VAR>)</I>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -