📄 fstream.html
字号:
<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="basic_filebuf::pos_type"><CODE>basic_filebuf::pos_type</CODE></A></H3><PRE>typedef typename 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="basic_filebuf::seekoff"><CODE>basic_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>basic_filebuf<Elem, Tr></CODE>, a stream position can be representedby an object of type<CODE><A HREF="stdio.html#fpos_t">fpos_t</A></CODE>, which stores anoffset and any state information needed to parse a<A HREF="lib_file.html#wide stream">wide stream</A>.Offset zero designates the first element of the stream.(An object of type<CODE><A HREF="streambu.html#basic_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#basic_streambuf::pubseekoff">pubseekoff</A></CODE>or<CODE><A HREF="streambu.html#basic_streambuf::pubseekpos">pubseekpos</A></CODE>.Calls to <CODE>pubseekoff</CODE> (and hence to <CODE>seekoff</CODE>)have various limitations for<A HREF="lib_file.html#text stream">text streams</A>,<A HREF="lib_file.html#binary stream">binary streams</A>, and<A HREF="lib_file.html#wide stream">wide 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, &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="basic_filebuf::seekpos"><CODE>basic_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>basic_filebuf<Elem, Tr></CODE>, a stream position can be representedby an object of type<CODE><A HREF="stdio.html#fpos_t">fpos_t</A></CODE>, which stores anoffset and any state information needed to parse a<A HREF="lib_file.html#wide stream">wide stream</A>.Offset zero designates the first element of the stream.(An object of type<CODE><A HREF="streambu.html#basic_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#basic_streambuf::pubseekoff">pubseekoff</A></CODE>or<CODE><A HREF="streambu.html#basic_streambuf::pubseekpos">pubseekpos</A></CODE>.Calls to <CODE>pubseekoff</CODE> (and hence to <CODE>seekoff</CODE>)have various limitations for<A HREF="lib_file.html#text stream">text streams</A>,<A HREF="lib_file.html#binary stream">binary streams</A>, and<A HREF="lib_file.html#wide stream">wide streams</A>.</P><P>For a wide stream,if any insertions have occured since the stream was opened, or since the lastcall to <CODE>streampos</CODE>, the function calls<CODE><A HREF="#basic_filebuf::overflow">overflow</A>()</CODE>.It also inserts any sequence needed to restore the<A HREF="charset.html#initial conversion state">initial conversion state</A>,by using the<A HREF="#file conversion facet">file conversion facet</A><CODE>fac</CODE> to call<CODE>fac.<A HREF="locale2.html#codecvt::unshift">unshift</A></CODE>as needed. Each element <CODE>byte</CODE> of type <I>char</I> thusproduced is written to the associated stream designated by thefile pointer <CODE>fp</CODE> as if by successive calls of the form<CODE><A HREF="stdio.html#fputc">fputc</A>(byte, fp)</CODE>.If the call to <CODE>fac.unshift</CODE> or any write fails,the function does not succeed.</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, &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="basic_filebuf::setbuf"><CODE>basic_filebuf::setbuf</CODE></A></H3><PRE>virtual basic_streambuf<Elem, Tr> *<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="basic_filebuf::sync"><CODE>basic_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="#basic_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="basic_filebuf::traits_type"><CODE>basic_filebuf::traits_type</CODE></A></H3><PRE>typedef Tr <B>traits_type</B>;</PRE><P>The type is a synonym for the template parameter <CODE>Tr</CODE>.</P><H3><A NAME="basic_filebuf::underflow"><CODE>basic_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>,and convert them to an element <CODE>ch</CODE> of type <CODE>Elem</CODE>by using the<A HREF="#file conversion facet">file conversion facet</A><CODE>fac</CODE> to call<CODE>fac.<A HREF="locale2.html#codecvt::in">in</A></CODE>as needed.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="basic_fstream"><CODE>basic_fstream</CODE></A></H2><PRE>template <class Elem, class Tr = char_traits<Elem> > class <B>basic_fstream</B> : public basic_iostream<Elem, Tr> {public: <B><A HREF="#basic_fstream::basic_fstream">basic_fstream</A></B>(); explicit <B><A HREF="#basic_fstream::basic_fstream">basic_fstream</A></B>(const char *filename, ios_base::openmode mode = ios_base::in | ios_base::out); basic_filebuf<Elem, Tr> *<B><A HREF="#basic_fstream::rdbuf">rdbuf</A></B>() const; bool <B><A HREF="#basic_fstream::is_open">is_open</A></B>() const; void <B><A HREF="#basic_fstream::open">open</A></B>(const char *filename, ios_base::openmode mode = ios_base::in | ios_base::out); void <B><A HREF="#basic_fstream::close">close</A></B>(); };</PRE><P>The template class describes an object that controlsinsertion and extraction of elements and encoded objects using a<A HREF="streambu.html#stream buffer">stream buffer</A> of class<CODE><A HREF="#basic_filebuf">basic_filebuf</A><Elem, Tr></CODE>,with elements of type <CODE>Elem</CODE>, whose<A HREF="string2.html#character traits">character traits</A> are determinedby the class <CODE>Tr</CODE>. The object stores an object of class<CODE>basic_filebuf<Elem, Tr></CODE>.</P><H3><A NAME="basic_fstream::basic_fstream"><CODE>basic_fstream::basic_fstream</CODE></A></H3><PRE><B>basic_fstream</B>();explicit <B>basic_fstream</B>(const char *filename, ios_base::openmode mode = ios_base::in | ios_base::out);</PRE><P>The first constructor initializes the base class by calling<CODE><A HREF="istream.html#basic_iostream">basic_iostream</A>(sb)</CODE>,where <CODE>sb</CODE> is the stored object of class<CODE><A HREF="#basic_filebuf">basic_filebuf</A><Elem, Tr></CODE>.It also initializes <CODE>sb</CODE> by calling<CODE><A HREF="#basic_filebuf::basic_filebuf">basic_filebuf</A><Elem,Tr>()</CODE>.</P><P>The second constructor initializes the base class by calling<CODE>basic_iostream(sb)</CODE>.It also initializes <CODE>sb</CODE> by calling<CODE><A HREF="#basic_filebuf::basic_filebuf">basic_filebuf</A><Elem,Tr>()</CODE>,then <CODE>sb.<A HREF="#basic_filebuf::open">open</A>(filename, mode)</CODE>.If the latter function returns a null pointer, the constructor calls<CODE><A HREF="ios.html#basic_ios::setstate">setstate</A>(failbit)</CODE>.</P><H3><A NAME="basic_fstream::close"><CODE>basic_fstream::close</CODE></A></H3><PRE> void<B>close</B>();</PRE><P>The member function calls<CODE><A HREF="#basic_fstream::rdbuf">rdbuf</A>()-><A HREF="#basic_filebuf::close">close</A>()</CODE>.</P><H3><A NAME="basic_fstream::is_open"><CODE>basic_fstream::is_open</CODE></A></H3><PRE>bool <B>is_open</B>();</PRE><P>The member function returns<CODE><A HREF="#basic_fstream::rdbuf">rdbuf</A>()-><A HREF="#basic_filebuf::is_open">is_open</A>()</CODE>.</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -