fstream.html

来自「ST20 Embedded Toolset R2.0.5用于开发基于ST20芯片」· HTML 代码 · 共 555 行 · 第 1/2 页

HTML
555
字号
<A HREF="lib_file.html#text stream">text streams</A>and <A HREF="lib_file.html#binary stream">binary streams</A>.</P><P>If the<A HREF="#file pointer">file pointer</A> <CODE>fp</CODE>is a null pointer, the function fails. Otherwise, it endeavorsto alter the stream position by calling<CODE><A HREF="stdio.html#fseek">fseek</A>(fp, off, way)</CODE>.If that function succeeds and the resultant position<CODE>fposn</CODE> can be determined by calling<CODE><A HREF="stdio.html#fgetpos">fgetpos</A>(fp, &amp;fposn)</CODE>,the function succeeds. If the function succeeds, it returnsa value of type <CODE>pos_type</CODE> containing <CODE>fposn</CODE>.Otherwise, it returns an invalid stream position.</P><H3><A NAME="filebuf::seekpos"><CODE>filebuf::seekpos</CODE></A></H3><PRE>virtual pos_type <B>seekpos</B>(pos_type pos,    ios_base::openmode which =        ios_base::in | ios_base::out);</PRE><P>The protected virtual member function endeavors to alter the currentpositions for the controlled streams. For an object of class<CODE>filebuf</CODE>, a stream position can be representedby an object of type<CODE><A HREF="stdio.html#fpos_t">fpos_t</A></CODE>.Offset zero designates the first element of the stream.(An object of type<CODE><A HREF="streambu.html#streambuf::pos_type">pos_type</A></CODE>stores at least an <CODE>fpos_t</CODE> object.)</P><P>For a file opened for both reading and writing,both the input and output streams are positioned in tandem. To<A HREF="lib_file.html#Stream States">switch</A>between inserting and extracting, you must call either<CODE><A HREF="streambu.html#streambuf::pubseekoff">pubseekoff</A></CODE>or<CODE><A HREF="streambu.html#streambuf::pubseekpos">pubseekpos</A></CODE>.Calls to <CODE>pubseekoff</CODE> (and hence to <CODE>seekoff</CODE>)have various limitations forboth <A HREF="lib_file.html#text stream">text streams</A>and <A HREF="lib_file.html#binary stream">binary streams</A>.</P><P>If the<A HREF="#file pointer">file pointer</A> <CODE>fp</CODE>is a null pointer, the function fails. Otherwise, it endeavorsto alter the stream position by calling<CODE><A HREF="stdio.html#fsetpos">fsetpos</A>(fp, &amp;fposn)</CODE>,where <CODE>fposn</CODE> is the <CODE>fpos_t</CODE> object storedin <CODE>pos</CODE>. If that function succeeds, the function returns<CODE>pos</CODE>. Otherwise, it returns an invalid stream position.</P><H3><A NAME="filebuf::setbuf"><CODE>filebuf::setbuf</CODE></A></H3><PRE>virtual streambuf    *<B>setbuf</B>(Elem *buffer, streamsize count);</PRE><P>The protected member function returns zero if the<A HREF="#file pointer">file pointer</A> <CODE>fp</CODE>is a null pointer. Otherwise, it calls<CODE><A HREF="stdio.html#setvbuf">setvbuf</A>(fp, (char *)buffer,<A HREF="stdio.html#_IOFBF">_IOFBF</A>, count * sizeof (Elem))</CODE>to offer the array of <CODE>count</CODE> elements beginning at <CODE>buffer</CODE>as a buffer for the stream. If that function returns a nonzero value,the function returns a null pointer. Otherwise, it returns <CODE>this</CODE>to signal success.</P><H3><A NAME="filebuf::sync"><CODE>filebuf::sync</CODE></A></H3><PRE>int <B>sync</B>();</PRE><P>The protected member function returns zero if the<A HREF="#file pointer">file pointer</A> <CODE>fp</CODE>is a null pointer. Otherwise, it returns zero only if calls to both<CODE><A HREF="#filebuf::overflow">overflow</A>()</CODE> and<CODE><A HREF="stdio.html#fflush">fflush</A>(fp)</CODE>succeed in flushing any pending output to the stream.</P><H3><A NAME="filebuf::traits_type"><CODE>filebuf::traits_type</CODE></A></H3><PRE>typedef char_traits <B>traits_type</B>;</PRE><P>The type is a synonym for<CODE><A HREF="string2.html#char_traits">char_traits</A></CODE>.</P><H3><A NAME="filebuf::underflow"><CODE>filebuf::underflow</CODE></A></H3><PRE>virtual int_type <B>underflow</B>();</PRE><P>The protected virtual member function endeavors to extract the currentelement <CODE>ch</CODE> from the input stream, and return the element as<CODE>traits_type::<A HREF="string2.html#char_traits::to_int_type">to_int_type</A>(ch)</CODE>.It can do so in various ways:</P><UL><LI>If a <A HREF="streambu.html#read position">read position</A> is available,it takes <CODE>ch</CODE> as the element stored in the read positionand advances the next pointer for the<A HREF="streambu.html#input buffer">input buffer</A>.<LI>It can read one or more elements of type <I>char,</I>as if by successive calls of the form<CODE><A HREF="stdio.html#fgetc">fgetc</A>(fp)</CODE>.If any read or conversion fails,the function does not succeed.</LI></UL><P>If the function cannot succeed, it returns<CODE>traits_type::<A HREF="string2.html#char_traits::eof">eof</A>()</CODE>. Otherwise,it returns <CODE>ch</CODE>, converted as described above.</P><H2><A NAME="ifstream"><CODE>ifstream</CODE></A></H2><PRE>class <B>ifstream</B> : public istream {public:    filebuf *<B><A HREF="#ifstream::rdbuf">rdbuf</A></B>() const;    <B><A HREF="#ifstream::ifstream">ifstream</A></B>();    explicit <B><A HREF="#ifstream::ifstream">ifstream</A></B>(const char *filename,        ios_base::openmode mode = ios_base::in);    bool <B><A HREF="#ifstream::is_open">is_open</A></B>() const;    void <B><A HREF="#ifstream::open">open</A></B>(const char *filename,        ios_base::openmode mode = ios_base::in);    void <B><A HREF="#ifstream::close">close</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> of class<CODE><A HREF="#filebuf">filebuf</A></CODE>.The object stores an object of class<CODE>filebuf</CODE>.</P><H3><A NAME="ifstream::ifstream"><CODE>ifstream::ifstream</CODE></A></H3><PRE><B>ifstream</B>();explicit <B>ifstream</B>(const char *filename,    ios_base::openmode mode = ios_base::in);</PRE><P>The first constructor initializes the base class by calling<CODE><A HREF="istream.html#istream::istream">istream</A>(sb)</CODE>,where <CODE>sb</CODE> is the stored object of class<CODE><A HREF="#filebuf">filebuf</A></CODE>.It also initializes <CODE>sb</CODE> by calling<CODE><A HREF="#filebuf::filebuf">filebuf</A>()</CODE>.</P><P>The second constructor initializes the base class by calling<CODE>istream(sb)</CODE>.It also initializes <CODE>sb</CODE> by calling<CODE><A HREF="#filebuf::filebuf">filebuf</A>()</CODE>,then <CODE>sb.<A HREF="#filebuf::open">open</A>(filename, mode| ios_base::in)</CODE>. If the latter function returns a nullpointer, the constructor calls<CODE><A HREF="ios.html#ios::setstate">setstate</A>(failbit)</CODE>.</P><H3><A NAME="ifstream::close"><CODE>ifstream::close</CODE></A></H3><PRE>void <B>close</B>();</PRE><P>The member function calls<CODE><A HREF="#ifstream::rdbuf">rdbuf</A>()-&gt;<A HREF="#filebuf::close">close</A>()</CODE>.</P><H3><A NAME="ifstream::is_open"><CODE>ifstream::is_open</CODE></A></H3><PRE>bool <B>is_open</B>();</PRE><P>The member function returns<CODE><A HREF="#ifstream::rdbuf">rdbuf</A>()-&gt;<A HREF="#filebuf::is_open">is_open</A>()</CODE>.</P><H3><A NAME="ifstream::open"><CODE>ifstream::open</CODE></A></H3><PRE>void <B>open</B>(const char *filename,    ios_base::openmode mode = ios_base::in);</PRE><P>The member function calls<CODE><A HREF="#ifstream::rdbuf">rdbuf</A>()-&gt;<A HREF="#filebuf::open">open</A>(filename, mode | ios_base::in)</CODE>.If that function returns a null pointer, the function calls<CODE><A HREF="ios.html#ios::setstate">setstate</A>(failbit)</CODE>.</P><H3><A NAME="ifstream::rdbuf"><CODE>ifstream::rdbuf</CODE></A></H3><PRE>filebuf *<B>rdbuf</B>() const</PRE><P>The member function returns the address of the stored stream buffer.</P><H2><A NAME="ofstream"><CODE>ofstream</CODE></A></H2><PRE>class <B>ofstream</B> : public ostream {public:    filebuf *<B><A HREF="#ofstream::rdbuf">rdbuf</A></B>() const;    <B><A HREF="#ofstream::ofstream">ofstream</A></B>();    explicit <B><A HREF="#ofstream::ofstream">ofstream</A></B>(const char *filename,        ios_base::openmode mode = ios_base::out);    bool <B><A HREF="#ofstream::is_open">is_open</A></B>() const;    void <B><A HREF="#ofstream::open">open</A></B>(const char *filename,        ios_base::openmode mode = ios_base::out);    void <B><A HREF="#ofstream::close">close</A></B>();    };</PRE><P>The class describes an object that controlsinsertion of elements and encoded objects into a<A HREF="streambu.html#stream buffer">stream buffer</A> of class<CODE><A HREF="#filebuf">filebuf</A></CODE>.The object stores an object of class<CODE>filebuf</CODE>.</P><H3><A NAME="ofstream::ofstream"><CODE>ofstream::ofstream</CODE></A></H3><PRE><B>ofstream</B>();explicit <B>ofstream</B>(const char *filename,    ios_base::openmode which = ios_base::out);</PRE><P>The first constructor initializes the base class by calling<CODE><A HREF="ostream.html#ostream::ostream">ostream</A>(sb)</CODE>,where <CODE>sb</CODE> is the stored object of class<CODE><A HREF="#filebuf">filebuf</A>&lt;Elem, Tr&gt;</CODE>.It also initializes <CODE>sb</CODE> by calling<CODE><A HREF="#filebuf::filebuf">filebuf</A>()</CODE>.</P><P>The second constructor initializes the base class by calling<CODE>ostream(sb)</CODE>.It also initializes <CODE>sb</CODE> by calling<CODE><A HREF="#filebuf::filebuf">filebuf</A>()</CODE>,then <CODE>sb.<A HREF="#filebuf::open">open</A>(filename, mode| ios_base::out)</CODE>. If the latter function returns a nullpointer, the constructor calls<CODE><A HREF="ios.html#ios::setstate">setstate</A>(failbit)</CODE>.</P><H3><A NAME="ofstream::close"><CODE>ofstream::close</CODE></A></H3><PRE>void <B>close</B>();</PRE><P>The member function calls<CODE><A HREF="#ofstream::rdbuf">rdbuf</A>()-&gt;<A HREF="#filebuf::close">close</A>()</CODE>.</P><H3><A NAME="ofstream::is_open"><CODE>ofstream::is_open</CODE></A></H3><PRE>bool <B>is_open</B>();</PRE><P>The member function returns<CODE><A HREF="#ofstream::rdbuf">rdbuf</A>()-&gt;<A HREF="#filebuf::is_open">is_open</A>()</CODE>.</P><H3><A NAME="ofstream::open"><CODE>ofstream::open</CODE></A></H3><PRE>void <B>open</B>(const char *filename,    ios_base::openmode mode = ios_base::out);</PRE><P>The member function calls<CODE><A HREF="#ofstream::rdbuf">rdbuf</A>()-&gt;<A HREF="#filebuf::open">open</A>(filename, mode | ios_base::out)</CODE>.If that function returns a null pointer, the function calls<CODE><A HREF="ios.html#ios::setstate">setstate</A>(failbit)</CODE>.</P><H3><A NAME="ofstream::rdbuf"><CODE>ofstream::rdbuf</CODE></A></H3><PRE>filebuf *<B>rdbuf</B>() const</PRE><P>The member function returns the address of the storedstream buffer.</P><HR><P>See also the<B><A HREF="index.html#Table of Contents">Table of Contents</A></B> and the<B><A HREF="_index.html">Index</A></B>.</P><P><I><A HREF="crit_pjp.html">Copyright</A> &#169; 1992-2002by P.J. Plauger. All rights reserved.</I></P><!--V4.01:1125--></BODY></HTML>

⌨️ 快捷键说明

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