fstream.html

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

HTML
555
字号
<HTML><HEAD><TITLE>&lt;fstream&gt;</TITLE></HEAD><BODY><H1><A NAME="&lt;fstream&gt;"><CODE>&lt;fstream&gt;</CODE></A></H1><HR><P>Include the <A HREF="lib_cpp.html#iostreams">iostreams</A>standard header <B><CODE>&lt;fstream&gt;</CODE></B>to define several classes that supportiostreams operations onsequences stored in external<A HREF="lib_file.html#files">files</A>.</P><PRE>        // DECLARATIONSclass <B><A HREF="#filebuf">filebuf</A></B>;class <B><A HREF="#ifstream">ifstream</A></B>;class <B><A HREF="#ofstream">ofstream</A></B>;        // END OF DECLARATIONS</PRE><H2><A NAME="filebuf"><CODE>filebuf</CODE></A></H2><PRE>class <B>filebuf</B> : public streambuf {public:    typedef typename streambuf&lt;Elem, Tr&gt;::char_type        char_type;    typedef typename streambuf&lt;Elem, Tr&gt;::traits_type        traits_type;    typedef typename streambuf&lt;Elem, Tr&gt;::int_type        int_type;    typedef typename streambuf&lt;Elem, Tr&gt;::pos_type        pos_type;    typedef typename streambuf&lt;Elem, Tr&gt;::off_type        off_type;    <B><A HREF="#filebuf::filebuf">filebuf</A></B>();    bool <B><A HREF="#filebuf::is_open">is_open</A></B>() const;    filebuf *<B><A HREF="#filebuf::open">open</A></B>(const char *filename,        ios_base::openmode mode);    filebuf *<B><A HREF="#filebuf::close">close</A></B>();protected:    virtual pos_type <B><A HREF="#filebuf::seekoff">seekoff</A></B>(off_type off,        ios_base::seekdir way,        ios_base::openmode which =            ios_base::in | ios_base::out);    virtual pos_type <B><A HREF="#filebuf::seekpos">seekpos</A></B>(pos_type pos,        ios_base::openmode which =            ios_base::in | ios_base::out);    virtual int_type <B><A HREF="#filebuf::underflow">underflow</A></B>();    virtual int_type <B><A HREF="#filebuf::pbackfail">pbackfail</A></B>(int_type meta =        traits_type::eof());    virtual int_type <B><A HREF="#filebuf::overflow">overflow</A></B>(int_type meta =        traits_type::eof());    virtual int <B><A HREF="#filebuf::sync">sync</A></B>();    virtual streambuf        *<B><A HREF="#filebuf::setbuf">setbuf</A></B>(Elem *buffer, streamsize count);    };</PRE><P>The classdescribes a <B><A HREF="streambu.html#stream buffer">stream buffer</A></B> that controlsthe transmission of elements to and from a sequence of elementsstored in an external<A HREF="lib_file.html#files">file</A>.</P><P>An object of class<CODE>filebuf</CODE> stores a<B><A NAME="file pointer">file pointer</A></B>, which designates the<CODE><A HREF="stdio.html#FILE">FILE</A></CODE> objectthat controls the <B><A HREF="lib_file.html#stream">stream</A></B>associated with an<A HREF="lib_file.html#file open">open</A> file.</P><H3><A NAME="filebuf::filebuf"><CODE>filebuf::filebuf</CODE></A></H3><PRE><B>filebuf</B>();</PRE><P>The constructor stores a null pointer in all the pointerscontrolling the<A HREF="streambu.html#input buffer">input buffer</A> and the<A HREF="streambu.html#output buffer">output buffer</A>. Italso stores a null pointer in the<A HREF="#file pointer">file pointer</A>.</P><H3><A NAME="filebuf::char_type"><CODE>filebuf::char_type</CODE></A></H3><PRE>typedef char <B>char_type</B>;</PRE><P>The type is a synonym for <I>char.</I></P><H3><A NAME="filebuf::close"><CODE>filebuf::close</CODE></A></H3><PRE>filebuf *<B>close</B>();</PRE><P>The member function returns a null pointer if the<A HREF="#file pointer">file pointer</A> <CODE>fp</CODE>is a null pointer. Otherwise, it calls<CODE><A HREF="stdio.html#fclose">fclose</A>(fp)</CODE>.If that function returns a nonzero value, the functionreturns a null pointer. Otherwise, it returns <CODE>this</CODE>to indicate that the file was successfully<A HREF="lib_file.html#file close">closed</A>.</P><H3><A NAME="filebuf::int_type"><CODE>filebuf::int_type</CODE></A></H3><PRE>typedef traits_type::int_type <B>int_type</B>;</PRE><P>The type is a synonym for<CODE>traits_type::<A HREF="string2.html#char_traits::int_type">int_type</A></CODE>.</P><H3><A NAME="filebuf::is_open"><CODE>filebuf::is_open</CODE></A></H3><PRE>bool <B>is_open</B>();</PRE><P>The member function returns true if the<A HREF="#file pointer">file pointer</A> is not a null pointer.</P><H3><A NAME="filebuf::off_type"><CODE>filebuf::off_type</CODE></A></H3><PRE>typedef traits_type::off_type <B>off_type</B>;</PRE><P>The type is a synonym for<CODE>traits_type::<A HREF="string2.html#char_traits::off_type">off_type</A></CODE>.</P><H3><A NAME="filebuf::open"><CODE>filebuf::open</CODE></A></H3><PRE>filebuf *<B>open</B>(const char *filename,    ios_base::openmode mode);</PRE><P>The member function endeavors to open the file with<A HREF="lib_over.html#filename">filename</A> <CODE>filename</CODE>, by calling<CODE><A HREF="stdio.html#fopen">fopen</A>(filename, strmode)</CODE>. Here<CODE>strmode</CODE> is determined from <CODE>mode &amp;~(<A HREF="ios.html#ios_base::ate">ate</A> &amp; |<A HREF="ios.html#ios_base::binary">binary</A>)</CODE>:</P><UL><LI><CODE>ios_base::<A HREF="ios.html#ios_base::in">in</A></CODE>becomes <CODE>"r"</CODE> (open existing file for reading).</LI><LI><CODE>ios_base::<A HREF="ios.html#ios_base::out">out</A></CODE> or<CODE>ios_base::out |ios_base::<A HREF="ios.html#ios_base::trunc">trunc</A></CODE>becomes <CODE>"w"</CODE> (truncate existing file or create for writing).</LI><LI><CODE>ios_base::out |ios_base::<A HREF="ios.html#ios_base::app">app</A></CODE>becomes <CODE>"a"</CODE> (open existing file for appending all writes).</LI><LI><CODE>ios_base::in | ios_base::out</CODE>becomes <CODE>"r+"</CODE> (open existing file for reading and writing).</LI><LI><CODE>ios_base::in | ios_base::out |ios_base::trunc</CODE> becomes <CODE>"w+"</CODE>(truncate existing file or create for reading and writing).</LI><LI><CODE>ios_base::in | ios_base::out |ios_base::app</CODE> becomes <CODE>"a+"</CODE>(open existing file for reading and for appending all writes).</LI></UL><P>If <CODE>mode &amp; ios_base::binary</CODE> is nonzero,the function appends <CODE>b</CODE> to <CODE>strmode</CODE>to open a <A HREF="lib_file.html#binary stream">binary stream</A>instead of a <A HREF="lib_file.html#text stream">text stream</A>.It then stores the value returned by <CODE>fopen</CODE> in the<A HREF="#file pointer">file pointer</A> <CODE>fp</CODE>. If<CODE>mode &amp; ios_base::ate</CODE> is nonzero and thefile pointer is not a null pointer, the function calls<CODE><A HREF="stdio.html#fseek">fseek</A>(fp, 0,<A HREF="stdio.html#SEEK_END">SEEK_END</A>)</CODE> toposition the stream at end-of-file. If that positioning operationfails, the function calls<CODE><A HREF="#filebuf::close">close</A>(fp)</CODE> andstores a null pointer in the file pointer.</P><P>If the file pointer is a null pointer, the function returnsa null pointer. Otherwise, it returns <CODE>this</CODE>.</P><H3><A NAME="filebuf::overflow"><CODE>filebuf::overflow</CODE></A></H3><PRE>virtual int_type <B>overflow</B>(int_type meta =    traits_type::eof());</PRE><P>If <CODE>meta !=traits_type::<A HREF="string2.html#char_traits::eof">eof</A>()</CODE>,the protected virtual member function endeavors to insert the element<CODE>ch = traits_type::<A HREF="string2.html#char_traits::to_char_type">to_char_type</A>(meta)</CODE>into the<A HREF="streambu.html#output buffer">output buffer</A>.It can do so in various ways:</P><UL><LI>If a <A HREF="streambu.html#write position">write position</A>is available, it can store the element into the write positionand increment the next pointer for the output buffer.</LI><LI>It can make a write position available by allocatingnew or additional storage for the output buffer.</LI><LI>It can write any pending output in the output buffer, followedby <CODE>ch</CODE>, to the associated stream designated by the<A HREF="#file pointer">file pointer</A> <CODE>fp</CODE>as if by successive calls of the form<CODE><A HREF="stdio.html#fputc">fputc</A>(ch, fp)</CODE>.If any conversion or write fails, the function does not succeed.</LI></UL><P>If the function cannot succeed, it returns <CODE>traits_type::eof()</CODE>.Otherwise, it returns<CODE>traits_type::<A HREF="string2.html#char_traits::not_eof">not_eof</A>(meta)</CODE>.</P><H3><A NAME="filebuf::pbackfail"><CODE>filebuf::pbackfail</CODE></A></H3><PRE>virtual int_type <B>pbackfail</B>(int_type meta =    traits_type::eof());</PRE><P>The protected virtual member function endeavors to put back an elementinto the<A HREF="streambu.html#input buffer">input buffer</A>,then make it the current element (pointed toby the next pointer). If <CODE>meta ==traits_type::<A HREF="string2.html#char_traits::eof">eof</A>()</CODE>,the element to push back is effectively the one already in the streambefore the current element. Otherwise, that element is replaced by<CODE>ch =traits_type::<A HREF="string2.html#char_traits::to_char_type">to_char_type</A>(meta)</CODE>.The function can put back an element in various ways:</P><UL><LI>If a <A HREF="streambu.html#putback position">putback position</A>is available, and the element stored there compares equal to <CODE>ch</CODE>,it can simply decrement the next pointer for the input buffer.</LI><LI>If the function can make a putback position available,it can do so, set the next pointer to point at that position,and store <CODE>ch</CODE> in that position.</LI><LI>If the function can push back an element onto the input stream,it can do so, such as by calling<CODE><A HREF="stdio.html#ungetc">ungetc</A></CODE> for an elementof type <I>char.</I></LI></UL><P>If the function cannot succeed, it returns<CODE>traits_type::eof()</CODE>. Otherwise, it returns<CODE>traits_type::<A HREF="string2.html#char_traits::not_eof">not_eof</A>(meta)</CODE>.</P><H3><A NAME="filebuf::pos_type"><CODE>filebuf::pos_type</CODE></A></H3><PRE>typedef traits_type::pos_type <B>pos_type</B>;</PRE><P>The type is a synonym for<CODE>traits_type::<A HREF="string2.html#char_traits::pos_type">pos_type</A></CODE>.</P><H3><A NAME="filebuf::seekoff"><CODE>filebuf::seekoff</CODE></A></H3><PRE>virtual pos_type <B>seekoff</B>(off_type off,    ios_base::seekdir way,    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 for

⌨️ 快捷键说明

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