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

📄 iostream.html

📁 vxworks相关论文
💻 HTML
📖 第 1 页 / 共 5 页
字号:
file, just as described for <CODE>ifstream::ifstream</CODE>.</P><P>The last optional argument <VAR>prot</VAR> specifies the file protection (bydefault <SAMP>`644'</SAMP>).</DL></P><P><DL><DT><U>Destructor:</U>  <B>ofstream::~ofstream</B> <I>()</I><DD><A NAME="IDX140"></A>The files associated with <CODE>ofstream</CODE> objects are closed when thecorresponding object is destroyed.</DL></P><P><DL><DT><U>Method:</U> void <B>ofstream::open</B> <I>(const char* <VAR>fname</VAR> [, int <VAR>mode</VAR> [, int <VAR>prot</VAR>]])</I><DD><A NAME="IDX141"></A>Open a file explicitly after the associated <CODE>ofstream</CODE> objectalready exists (for instance, after using the default constructor).  Thearguments, options and defaults all have the same meanings as in thefully specified <CODE>ofstream</CODE> constructor.</DL></P><P><A NAME="IDX142"></A><A NAME="IDX143"></A>The class <CODE>fstream</CODE> combines the facilities of <CODE>ifstream</CODE> and<CODE>ofstream</CODE>, just as <CODE>iostream</CODE> combines <CODE>istream</CODE> and<CODE>ostream</CODE>.</P><P><A NAME="IDX144"></A><A NAME="IDX145"></A>The class <CODE>fstreambase</CODE> underlies both <CODE>ifstream</CODE> and<CODE>ofstream</CODE>.  They both inherit this additional method:</P><P><DL><DT><U>Method:</U> void <B>fstreambase::close</B> <I>()</I><DD><A NAME="IDX146"></A>Close the file associated with this object, and set <CODE>ios::fail</CODE> inthis object to mark the event.</DL></P><H2><A NAME="SEC25" HREF="iostream_toc.html#TOC25">Reading and writing in memory</A></H2><P><A NAME="IDX147"></A><A NAME="IDX148"></A><A NAME="IDX149"></A><A NAME="IDX150"></A><A NAME="IDX151"></A><A NAME="IDX152"></A><A NAME="IDX153"></A><A NAME="IDX154"></A><A NAME="IDX155"></A><A NAME="IDX156"></A>The classes <CODE>istrstream</CODE>, <CODE>ostrstream</CODE>, and <CODE>strstream</CODE>provide some additional features for reading and writing strings inmemory--both static strings, and dynamically allocated strings.  Theunderlying class <CODE>strstreambase</CODE> provides some features common toall three; <CODE>strstreambuf</CODE> underlies that in turn.</P><P><DL><DT><U>Constructor:</U>  <B>istrstream::istrstream</B> <I>(const char* <VAR>str</VAR> [, int <VAR>size</VAR>])</I><DD><A NAME="IDX157"></A>Associate the new input string class <CODE>istrstream</CODE> with an existingstatic string starting at <VAR>str</VAR>, of size <VAR>size</VAR>.  If you do notspecify <VAR>size</VAR>, the string is treated as a <CODE>NUL</CODE> terminated string.</DL></P><P><DL><DT><U>Constructor:</U>  <B>ostrstream::ostrstream</B> <I>()</I><DD><A NAME="IDX158"></A>Create a new stream for output to a dynamically managed string, whichwill grow as needed.</DL></P><P><DL><DT><U>Constructor:</U>  <B>ostrstream::ostrstream</B> <I>(char* <VAR>str</VAR>, int <VAR>size</VAR> [,int <VAR>mode</VAR>])</I><DD><A NAME="IDX159"></A>A new stream for output to a statically defined string of length<VAR>size</VAR>, starting at <VAR>str</VAR>.  You may optionally specify one ofthe modes described for <CODE>ifstream::ifstream</CODE>; if you do not specifyone, the new stream is simply open for output, with mode <CODE>ios::out</CODE>.</DL></P><P><DL><DT><U>Method:</U> int <B>ostrstream::pcount</B> <I>()</I><DD><A NAME="IDX160"></A>Report the current length of the string associated with this <CODE>ostrstream</CODE>.</DL></P><P><DL><DT><U>Method:</U> char* <B>ostrstream::str</B> <I>()</I><DD><A NAME="IDX161"></A>A pointer to the string managed by this <CODE>ostrstream</CODE>.  Implies<SAMP>`ostrstream::freeze()'</SAMP>.</P><P>Note that if you want the string to be nul-terminated,you must do that yourself (perhaps by writing ends to the stream).</DL></P><P><DL><DT><U>Method:</U> void <B>ostrstream::freeze</B> <I>([int <VAR>n</VAR>])</I><DD><A NAME="IDX162"></A>If <VAR>n</VAR> is nonzero (the default), declare that the string associatedwith this <CODE>ostrstream</CODE> is not to change dynamically; while frozen,it will not be reallocated if it needs more space, and it will not bedeallocated when the <CODE>ostrstream</CODE> is destroyed.  Use<SAMP>`freeze(1)'</SAMP> if you refer to the string as a pointer after creatingit via <CODE>ostrstream</CODE> facilities.</P><P><SAMP>`freeze(0)'</SAMP> cancels this declaration, allowing a dynamicallyallocated string to be freed when its <CODE>ostrstream</CODE> is destroyed.</P><P>If this <CODE>ostrstream</CODE> is already static--that is, if it was createdto manage an existing statically allocated string---<CODE>freeze</CODE> isunnecessary, and has no effect.</DL></P><P><DL><DT><U>Method:</U> int <B>ostrstream::frozen</B> <I>()</I><DD><A NAME="IDX163"></A>Test whether <CODE>freeze(1)</CODE> is in effect for this string.</DL></P><P><DL><DT><U>Method:</U> strstreambuf* <B>strstreambase::rdbuf</B> <I>()</I><DD><A NAME="IDX164"></A>A pointer to the underlying <CODE>strstreambuf</CODE>.</DL></P><H1><A NAME="SEC26" HREF="iostream_toc.html#TOC26">Using the <CODE>streambuf</CODE> Layer</A></H1><P>The <CODE>istream</CODE> and <CODE>ostream</CODE> classes are meant to handleconversion between objects in your program and their textual representation.</P><P>By contrast, the underlying <CODE>streambuf</CODE> class is for transferringraw bytes between your program, and input sources or output sinks.Different <CODE>streambuf</CODE> subclasses connect to different kinds ofsources and sinks.</P><P>The GNU implementation of <CODE>streambuf</CODE> is still evolving; wedescribe only some of the highlights.</P><H2><A NAME="SEC27" HREF="iostream_toc.html#TOC27">Areas of a <CODE>streambuf</CODE></A></H2><P>Streambuf buffer management is fairly sophisticated (this is anice way to say "complicated").  The standard protocolhas the following "areas":</P><UL><LI><A NAME="IDX165"></A>The <EM>put area</EM> contains characters waiting for output.<LI><A NAME="IDX166"></A>The <EM>get area</EM> contains characters available for reading.</UL><P>The GNU <CODE>streambuf</CODE> design extends this, but the details arestill evolving.</P><P>The following methods are used to manipulate these areas.These are all protected methods, which are intended to beused by virtual function in classes derived from <CODE>streambuf</CODE>.They are also all ANSI/ISO-standard, and the ugly namesare traditional.(Note that if a pointer points to the 'end' of an area,it means that it points to the character after the area.)</P><P><DL><DT><U>Method:</U> char* <B>streambuf::pbase</B> <I>() const</I><DD><A NAME="IDX167"></A>Returns a pointer to the start of the put area.</DL></P><P><DL><DT><U>Method:</U> char* <B>streambuf::epptr</B> <I>() const</I><DD><A NAME="IDX168"></A>Returns a pointer to the end of the put area.</DL></P><P><DL><DT><U>Method:</U> char* <B>streambuf::pptr</B> <I>() const</I><DD><A NAME="IDX169"></A>If <CODE>pptr() &#60; epptr ()</CODE>, the <CODE>pptr()</CODE>returns a pointer to the current put position.(In that case, the next write willoverwrite <CODE>*pptr()</CODE>, and increment <CODE>pptr()</CODE>.)Otherwise, there is no put position available(and the next character written will cause <CODE>streambuf::overflow</CODE>to be called).</DL></P><P><DL><DT><U>Method:</U> void <B>streambuf::pbump</B> <I>(int <VAR>N</VAR>)</I><DD><A NAME="IDX170"></A>Add <VAR>N</VAR> to the current put pointer.No error checking is done.</DL></P><P><DL><DT><U>Method:</U> void <B>streambuf::setp</B> <I>(char* <VAR>P</VAR>, char* <VAR>E</VAR>)</I><DD><A NAME="IDX171"></A>Sets the start of the put area to <VAR>P</VAR>, the end of the put area to <VAR>E</VAR>,and the current put pointer to <VAR>P</VAR> (also).</DL></P><P><DL><DT><U>Method:</U> char* <B>streambuf::eback</B> <I>() const</I><DD><A NAME="IDX172"></A>Returns a pointer to the start of the get area.</DL></P><P><DL><DT><U>Method:</U> char* <B>streambuf::egptr</B> <I>() const</I><DD><A NAME="IDX173"></A>Returns a pointer to the end of the get area.</DL></P><P><DL><DT><U>Method:</U> char* <B>streambuf::gptr</B> <I>() const</I><DD><A NAME="IDX174"></A>If <CODE>gptr() &#60; egptr ()</CODE>, then <CODE>gptr()</CODE>returns a pointer to the current get position.(In that case the next read will read <CODE>*gptr()</CODE>,and possibly increment <CODE>gptr()</CODE>.)Otherwise, there is no read position available(and the next read will cause <CODE>streambuf::underflow</CODE>to be called).</DL></P><P><DL><DT><U>Method:</U> void <B>streambuf:gbump</B> <I>(int <VAR>N</VAR>)</I><DD><A NAME="IDX175"></A>Add <VAR>N</VAR> to the current get pointer.No error checking is done.</DL></P><P><DL><DT><U>Method:</U> void <B>streambuf::setg</B> <I>(char* <VAR>B</VAR>, char* <VAR>P</VAR>, char* <VAR>E</VAR>)</I><DD><A NAME="IDX176"></A>Sets the start of the get area to <VAR>B</VAR>, the end of the get area to <VAR>E</VAR>,and the current put pointer to <VAR>P</VAR>.</DL></P><H2><A NAME="SEC28" HREF="iostream_toc.html#TOC28">Simple output re-direction by redefining <CODE>overflow</CODE></A></H2><P>Suppose you have a function <CODE>write_to_window</CODE> thatwrites characters to a <CODE>window</CODE> object.  If you want to use theostream function to write to it, here is one (portable) way to do it.This depends on the default buffering (if any).</P><PRE>#include &#60;iostream.h&#62;/* Returns number of characters successfully written to <VAR>win</VAR>. */extern int write_to_window (window* win, char* text, int length);class windowbuf : public streambuf {    window* win;  public:    windowbuf (window* w) { win = w; }    int sync ();    int overflow (int ch);    // Defining xsputn is an optional optimization.    // (streamsize was recently added to ANSI C++, not portable yet.)    streamsize xsputn (char* text, streamsize n);};int windowbuf::sync (){ streamsize n = pptr () - pbase ();  return (n &#38;&#38; write_to_window (win, pbase (), n) != n) ? EOF : 0;}int windowbuf::overflow (int ch){ streamsize n = pptr () - pbase ();  if (n &#38;&#38; sync ())    return EOF;  if (ch != EOF)    {      char cbuf[1];      cbuf[0] = ch;      if (write_to_window (win, cbuf, 1) != 1)        return EOF;    }  pbump (-n);  // Reset pptr().  return 0;}streamsize windowbuf::xsputn (char* text, streamsize n){ return sync () == EOF ? 0 : write_to_window (win, text, n); }intmain (int argc, char**argv){  window *win = ...;  windowbuf wbuf(win);  ostream wstr(&#38;wbuf);  wstr &#60;&#60; "Hello world!\n";}</PRE><H2><A NAME="SEC29" HREF="iostream_toc.html#TOC29">C-style formatting for <CODE>streambuf</CODE> objects</A></H2><P>The GNU <CODE>streambuf</CODE> class supports <CODE>printf</CODE>-likeformatting and scanning.</P><P><DL><DT><U>Method:</U> int <B>streambuf::form</B> <I>(const char *<VAR>format</VAR>, ...)</I><DD><A NAME="IDX177"></A>Similar to <CODE>fprintf(<VAR>file</VAR>, <VAR>format</VAR>, ...)</CODE>.The <VAR>format</VAR> is a <CODE>printf</CODE>-style format control string, which is usedto format the (variable number of) arguments, printing the result onthe <CODE>this</CODE> streambuf.  The result is the number of characters printed.</DL></P><P><DL><DT><U>Method:</U> int <B>streambuf::vform</B> <I>(const char *<VAR>format</VAR>, va_list <VAR>args</VAR>)</I><DD><A NAME="IDX178"></A>Similar to <CODE>vfprintf(<VAR>file</VAR>, <VAR>format</VAR>, <VAR>args</VAR>)</CODE>.The <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 onthe <CODE>this</CODE> streambuf.  The result is the number of characters printed.</DL></P><P><DL><DT><U>Method:</U> int <B>streambuf::scan</B> <I>(const char *<VAR>format</VAR>, ...)</I><DD><A NAME="IDX179"></A>Similar to <CODE>fscanf(<VAR>file</VAR>, <VAR>format</VAR>, ...)</CODE>.The <VAR>format</VAR> is a <CODE>scanf</CODE>-style format control string, which is usedto read the (variable number of) arguments from the <CODE>this</CODE> streambuf.The result is the number of items assigned, or <CODE>EOF</CODE> in case ofinput failure before any conversion.</DL></P><P><DL><DT><U>Method:</U> int <B>streambuf::vscan</B> <I>(const char *<VAR>format</VAR>, va_list <VAR>args</VAR>)</I><DD><A NAME="IDX180"></A>Like <CODE>streambuf::scan</CODE>, but takes a single <CODE>va_list</CODE> argument.</DL></P><H2><A NAME="SEC30" HREF="iostream_toc.html#TOC30">Wrappers for C <CODE>stdio</CODE></A></H2><P>A <EM>stdiobuf</EM> is a <CODE>streambuf</CODE> object that points toa <CODE>FILE</CODE> object (as defined by <CODE>stdio.h</CODE>).All <CODE>streambuf</CODE> operations on the <CODE>stdiobuf</CODE> are forwardedto the <CODE>FILE</CODE>.  Thus the <CODE>stdiobuf</CODE> object provides awrapper around a <CODE>FILE</CODE>, allowing use of <CODE>streambuf</CODE>operations on a <CODE>FILE</CODE>.  This can be useful when mixingC code with C++ code.</P><P>The pre-defined stream

⌨️ 快捷键说明

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