⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iostream.html

📁 vxworks相关论文
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<HTML><HEAD><!-- This HTML file has been created by texi2html 1.52     from iostream.texi on 3 March 1999 --><TITLE>The GNU C++ Iostream Library</TITLE></HEAD><BODY><H1>The GNU C++ Iostream Library</H1><H2>Reference Manual for <CODE>libio</CODE> Version 0.64</H2><ADDRESS>Per Bothner  <CODE><A HREF="mailto:bothner@cygnus.com">bothner@cygnus.com</A></CODE></ADDRESS><ADDRESS>Cygnus Support  <CODE><A HREF="mailto:doc@cygnus.com">doc@cygnus.com</A></CODE></ADDRESS><P><P><HR><P><P><P>Copyright (C) 1993 Free Software Foundation, Inc.</P><P><CODE>libio</CODE> includes software developed by the University ofCalifornia, Berkeley.</P><P><CODE>libio</CODE> uses floating-point software written by David M. Gay, whichincludes the following notice:</P><BLOCKQUOTE><P>The author of this software is David M. Gay.</P><P>Copyright (c) 1991 by AT&#38;T.</P><P>Permission to use, copy, modify, and distribute this software for anypurpose without fee is hereby granted, provided that this entire noticeis included in all copies of any software which is or includes a copyor modification of this software and in all copies of the supportingdocumentation for such software.</P><P>THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIEDWARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR AT&#38;T MAKES ANYREPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITYOF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.</BLOCKQUOTE><P>Permission is granted to make and distribute verbatim copies ofthis manual provided the copyright notice and this permission noticeare preserved on all copies.</P><P>Permission is granted to copy and distribute modified versions of thismanual under the conditions for verbatim copying, provided also that theentire resulting derived work is distributed under the terms of apermission notice identical to this one.</P><P>Permission is granted to copy and distribute translations of this manualinto another language, under the above conditions for modified versions.</P><H1><A NAME="SEC1" HREF="iostream_toc.html#TOC1">Introduction</A></H1><P>The iostream classes implement most of the features of AT&#38;T version 2.0iostream library classes, and most of the features of the ANSI X3J16library draft (which is based on the AT&#38;T design).</P><P>This manual is meant as a reference; for tutorial material on iostreams,see the corresponding section of any recent popular introduction to C++.</P><H2><A NAME="SEC2" HREF="iostream_toc.html#TOC2">Licensing terms for <CODE>libio</CODE></A></H2><P>Since the <CODE>iostream</CODE> classes are so fundamental to standard C++,the Free Software Foundation has agreed to a special exception to itsstandard license, when you link programs with <CODE>libio.a</CODE>:</P><BLOCKQUOTE><P>As a special exception, if you link this library with filescompiled with a GNU compiler to produce an executable, this does not causethe resulting executable to be covered by the GNU General Public License.This exception does not however invalidate any other reasons whythe executable file might be covered by the GNU General Public License.</BLOCKQUOTE><P>The code is under the GNU General Public License (version 2) forall other purposes than linking with this library; that means that youcan modify and redistribute the code as usual, but remember that if youdo, your modifications, and anything you link with the modified code,must be available to others on the same terms.</P><P>These functions are also available as part of the <CODE>libg++</CODE>library; if you link with that library instead of <CODE>libio</CODE>, theGNU Library General Public License applies.</P><H2><A NAME="SEC3" HREF="iostream_toc.html#TOC3">Acknowledgements</A></H2><P>Per Bothner wrote most of the <CODE>iostream</CODE> library, but some portionshave their origins elsewhere in the free software community.  HeinzSeidl wrote the IO manipulators.  The floating-point conversion softwareis by David M. Gay of AT&#38;T.  Some code was derived from parts of BSD4.4, which was written at the University of California, Berkeley.</P><P>The iostream classes are found in the <CODE>libio</CODE> library.  An earlyversion was originally distributed in <CODE>libg++</CODE>, and they are stillincluded there as well, for convenience if you need other <CODE>libg++</CODE>classes.  Doug Lea was the original author of <CODE>libg++</CODE>, and some ofthe file-management code still in <CODE>libio</CODE> is his.</P><P>Various people found bugs or offered suggestions.  Hongjiu Lu workedhard to use the library as the default stdio implementation for Linux,and has provided much stress-testing of the library.</P><H1><A NAME="SEC4" HREF="iostream_toc.html#TOC4">Operators and Default Streams</A></H1><P>The GNU iostream library, <TT>`libio'</TT>, implements the standardinput and output facilities for C++.  These facilities are roughlyanalogous (in their purpose and ubiquity, at least) with those definedby the C <TT>`stdio'</TT> functions.</P><P>Although these definitions come from a library, rather than being partof the "core language", they are sufficiently central to be specifiedin the latest working papers for C++.</P><P>You can use two operators defined in this library for basic input andoutput operations.  They are familiar from any C++ introductorytextbook: <CODE>&#60;&#60;</CODE> for output, and <CODE>&#62;&#62;</CODE> for input.  (Think of dataflowing in the direction of the "arrows".)</P><P>These operators are often used in conjunction with three streams thatare open by default: </P><P><DL><DT><U>Variable:</U> ostream <B>cout</B><DD><A NAME="IDX1"></A>The standard output stream, analogous to the C <CODE>stdout</CODE>.</DL></P><P><DL><DT><U>Variable:</U> istream <B>cin</B><DD><A NAME="IDX2"></A>The standard input stream, analogous to the C <CODE>stdin</CODE>.</DL></P><P><DL><DT><U>Variable:</U> ostream <B>cerr</B><DD><A NAME="IDX3"></A>An alternative output stream for errors, analogous to the C<CODE>stderr</CODE>.</DL></P><P>For example, this bare-bones C++ version of the traditional "hello"program uses <CODE>&#60;&#60;</CODE> and <CODE>cout</CODE>:</P><PRE>#include &#60;iostream.h&#62;int main(int argc, char **argv){  cout &#60;&#60; "Well, hi there.\n";  return 0;}</PRE><P>Casual use of these operators may be seductive, but--other than inwriting throwaway code for your own use--it is not necessarily simplerthan managing input and output in any other language.  For example,robust code should check the state of the input and output streamsbetween operations (for example, using the method <CODE>good</CODE>).See section <A HREF="iostream.html#SEC7">Checking the state of a stream</A>.  You may also need toadjust maximum input or output field widths, using manipulators like<CODE>setw</CODE> or <CODE>setprecision</CODE>.</P><P><DL><DT><U>Operator:</U> ostream <B>&#60;&#60;</B><DD><A NAME="IDX4"></A>Write output to an open output stream of class <CODE>ostream</CODE>.Defined by this library on any <VAR>object</VAR> of a C++ primitive type, andon other classes of the library.  You can overload the definition for anyof your own applications' classes.</P><P>Returns a reference to the implied argument <CODE>*this</CODE> (the open stream itwrites on), permitting statements like<PRE>cout &#60;&#60; "The value of i is " &#60;&#60; i &#60;&#60; "\n";</PRE></DL><P><DL><DT><U>Operator:</U> istream <B>&#62;&#62;</B><DD><A NAME="IDX5"></A>Read input from an open input stream of class <CODE>istream</CODE>.  Definedby this library on primitive numeric, pointer, and string types; you canextend the definition for any of your own applications' classes.</P><P>Returns a reference to the implied argument <CODE>*this</CODE> (the open streamit reads), permitting multiple inputs in one statement.</DL></P><H1><A NAME="SEC5" HREF="iostream_toc.html#TOC5">Stream Classes</A></H1><P>The previous chapter referred in passing to the classes <CODE>ostream</CODE>and <CODE>istream</CODE>, for output and input respectively.  These classesshare certain properties, captured in their base class <CODE>ios</CODE>.</P><H2><A NAME="SEC6" HREF="iostream_toc.html#TOC6">Shared properties: class <CODE>ios</CODE></A></H2><P>The base class <CODE>ios</CODE> provides methods to test and manage the stateof input or output streams.</P><P><CODE>ios</CODE> delegates the job of actually reading and writing bytes tothe abstract class <CODE>streambuf</CODE>, which is designed to providebuffered streams (compatible with C, in the GNU implementation).See section <A HREF="iostream.html#SEC26">Using the <CODE>streambuf</CODE> Layer</A>, for information onthe facilities available at the <CODE>streambuf</CODE> level.</P><P><DL><DT><U>Constructor:</U>  <B>ios::ios</B> <I>([streambuf* <VAR>sb</VAR> [, ostream* <VAR>tie</VAR>])</I><DD><A NAME="IDX6"></A>The <CODE>ios</CODE> constructor by default initializes a new <CODE>ios</CODE>, andif you supply a <CODE>streambuf</CODE> <VAR>sb</VAR> to associate with it, sets thestate <CODE>good</CODE> in the new <CODE>ios</CODE> object.  It also sets thedefault properties of the new object.</P><P>You can also supply an optional second argument <VAR>tie</VAR> to theconstructor: if present, it is an initial value for <CODE>ios::tie</CODE>, toassociate the new <CODE>ios</CODE> object with another stream.</DL></P><P><DL><DT><U>Destructor:</U>  <B>ios::~ios</B> <I>()</I><DD><A NAME="IDX7"></A>The <CODE>ios</CODE> destructor is virtual, permitting application-specificbehavior when a stream is closed--typically, the destructor frees anystorage associated with the stream and releases any other associatedobjects.</DL></P><H3><A NAME="SEC7" HREF="iostream_toc.html#TOC7">Checking the state of a stream</A></H3><P>Use this collection of methods to test for (or signal) errors and otherexceptional conditions of streams:</P><P><DL><DT><U>Method:</U> ios::operator void* <B>()</B> <I>const</I><DD><A NAME="IDX8"></A>You can do a quick check on the state of the most recent operation on astream by examining a pointer to the stream itself.  The pointer isarbitrary except for its truth value; it is true if no failures haveoccurred (<CODE>ios::fail</CODE> is not true).  For example, you might ask forinput on <CODE>cin</CODE> only if all prior output operations succeeded:</P><PRE>if (cout){  // Everything OK so far  cin &#62;&#62; new_value;  ...}</PRE></DL><P><DL><DT><U>Method:</U> ios::operator ! <B>()</B> <I>const</I><DD><A NAME="IDX9"></A>In case it is more convenient to check whether something has failed, theoperator <CODE>!</CODE> returns true if <CODE>ios::fail</CODE> is true (an operationhas failed).  For example,you might issue an error message if input failed:</P><PRE>if (!cin){  // Oops  cerr &#60;&#60; "Eh?\n";}</PRE></DL><P><DL><DT><U>Method:</U> iostate <B>ios::rdstate</B> <I>() const</I><DD><A NAME="IDX10"></A>Return the state flags for this stream.  The value is from theenumeration <CODE>iostate</CODE>.  You can test for any combination of</P><DL COMPACT><DT><CODE>goodbit</CODE><DD><A NAME="IDX11"></A>There are no indications of exceptional states on this stream.<DT><CODE>eofbit</CODE><DD><A NAME="IDX12"></A>End of file.<DT><CODE>failbit</CODE><DD><A NAME="IDX13"></A>An operation has failed on this stream; this usually indicates badformat of input.<DT><CODE>badbit</CODE><DD><A NAME="IDX14"></A>The stream is unusable.</DL></DL><P><DL><DT><U>Method:</U> void <B>ios::setstate</B> <I>(iostate <VAR>state</VAR>)</I><DD><A NAME="IDX15"></A><A NAME="IDX16"></A>Set the state flag for this stream to <VAR>state</VAR> <EM>in addition to</EM>any state flags already set.  Synonym (for upward compatibility):<CODE>ios::set</CODE>.</P><P>See <CODE>ios::clear</CODE> to set the stream state without regard to existingstate flags.  See <CODE>ios::good</CODE>, <CODE>ios::eof</CODE>, <CODE>ios::fail</CODE>,and <CODE>ios::bad</CODE>, to test the state.</DL></P><P><DL><DT><U>Method:</U> int <B>ios::good</B> <I>() const</I><DD><A NAME="IDX17"></A>Test the state flags associated with this stream; true if no errorindicators are set.</DL></P><P><DL><DT><U>Method:</U> int <B>ios::bad</B> <I>() const</I><DD><A NAME="IDX18"></A>Test whether a stream is marked as unusable.  (Whether<CODE>ios::badbit</CODE> is set.)</DL></P><P><DL><DT><U>Method:</U> int <B>ios::eof</B> <I>() const</I><DD><A NAME="IDX19"></A>True if end of file was reached on this stream.  (If <CODE>ios::eofbit</CODE>is set.)</DL>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -