# fstream

Go to the documentation of this file.

    00001 // File based streams -*- C++ -*-
    00002
    00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
    00004 // 2006, 2007, 2008
    00005 // Free Software Foundation, Inc.
    00006 //
    00007 // This file is part of the GNU ISO C++ Library.  This library is free
    00008 // software; you can redistribute it and/or modify it under the
    00009 // terms of the GNU General Public License as published by the
    00010 // Free Software Foundation; either version 2, or (at your option)
    00011 // any later version.
    00012
    00013 // This library is distributed in the hope that it will be useful,
    00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00016 // GNU General Public License for more details.
    00017
    00018 // You should have received a copy of the GNU General Public License
    00019 // along with this library; see the file COPYING.  If not, write to
    00020 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
    00021 // Boston, MA 02110-1301, USA.
    00022
    00023 // As a special exception, you may use this file as part of a free software
    00024 // library without restriction.  Specifically, if other files instantiate
    00025 // templates or use macros or inline functions from this file, or you compile
    00026 // this file and link it with other files to produce an executable, this
    00027 // file does not by itself cause the resulting executable to be covered by
    00028 // the GNU General Public License.  This exception does not however
    00029 // invalidate any other reasons why the executable file might be covered by
    00030 // the GNU General Public License.
    00031
    00032 /** @file fstream
    00033  *  This is a Standard C++ Library header.
    00034  */
    00035
    00036 //
    00037 // ISO C++ 14882: 27.8  File-based streams
    00038 //
    00039
    00040 #ifndef _GLIBCXX_FSTREAM
    00041 #define _GLIBCXX_FSTREAM 1
    00042
    00043 #pragma GCC system_header
    00044
    00045 #include <istream>
    00046 #include <ostream>
    00047 #include <bits/codecvt.h>
    00048 #include <cstdio>             // For BUFSIZ
    00049 #include <bits/basic_file.h>  // For __basic_file, __c_lock
    00050
    00051 _GLIBCXX_BEGIN_NAMESPACE(std)
    00052
    00053   // [27.8.1.1] template class basic_filebuf
    00054   /**
    00055    *  @brief  The actual work of input and output (for files).
    00056    *
    00057    *  This class associates both its input and output sequence with an
    00058    *  external disk file, and maintains a joint file position for both
    00059    *  sequences.  Many of its semantics are described in terms of similar
    00060    *  behavior in the Standard C Library's @c FILE streams.
    00061   */
    00062   // Requirements on traits_type, specific to this class:
    00063   // traits_type::pos_type must be fpos<traits_type::state_type>
    00064   // traits_type::off_type must be streamoff
    00065   // traits_type::state_type must be Assignable and DefaultConstructible,
    00066   // and traits_type::state_type() must be the initial state for codecvt.
    00067   template<typename _CharT, typename _Traits>
    00068     class basic_filebuf : public basic_streambuf<_CharT, _Traits>
    00069     {
    00070     public:
    00071       // Types:
    00072       typedef _CharT                                char_type;
    00073       typedef _Traits                               traits_type;
    00074       typedef typename traits_type::int_type        int_type;
    00075       typedef typename traits_type::pos_type        pos_type;
    00076       typedef typename traits_type::off_type        off_type;
    00077
    00078       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
    00079       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
    00080       typedef __basic_file<char>                __file_type;
    00081       typedef typename traits_type::state_type          __state_type;
    00082       typedef codecvt<char_type, char, __state_type>    __codecvt_type;
    00083
    00084       friend class ios_base; // For sync_with_stdio.
    00085
    00086     protected:
    00087       // Data Members:
    00088       // MT lock inherited from libio or other low-level io library.
    00089       __c_lock              _M_lock;
    00090
    00091       // External buffer.
    00092       __file_type       _M_file;
    00093
    00094       /// Place to stash in || out || in | out settings for current filebuf.
    00095       ios_base::openmode    _M_mode;
    00096
    00097       // Beginning state type for codecvt.
    00098       __state_type      _M_state_beg;
    00099
    00100       // During output, the state that corresponds to pptr(),
    00101       // during input, the state that corresponds to egptr() and
    00102       // _M_ext_next.
    00103       __state_type      _M_state_cur;
    00104
    00105       // Not used for output. During input, the state that corresponds
    00106       // to eback() and _M_ext_buf.
    00107       __state_type      _M_state_last;
    00108
    00109       /// Pointer to the beginning of internal buffer.
    00110       char_type*        _M_buf;
    00111
    00112       /**
    00113        *  Actual size of internal buffer. This number is equal to the size
    00114        *  of the put area + 1 position, reserved for the overflow char of
    00115        *  a full area.
    00116       */
    00117       size_t            _M_buf_size;
    00118
    00119       // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
    00120       bool          _M_buf_allocated;
    00121
    00122       /**
    00123        *  _M_reading == false && _M_writing == false for 'uncommitted' mode;
    00124        *  _M_reading == true for 'read' mode;
    00125        *  _M_writing == true for 'write' mode;
    00126        *
    00127        *  NB: _M_reading == true && _M_writing == true is unused.
    00128       */
    00129       bool                      _M_reading;
    00130       bool                      _M_writing;
    00131
    00132       //@{
    00133       /**
    00134        *  Necessary bits for putback buffer management.
    00135        *
    00136        *  @note pbacks of over one character are not currently supported.
    00137       */
    00138       char_type         _M_pback;
    00139       char_type*        _M_pback_cur_save;
    00140       char_type*        _M_pback_end_save;
    00141       bool          _M_pback_init;
    00142       //@}
    00143
    00144       // Cached codecvt facet.
    00145       const __codecvt_type*     _M_codecvt;
    00146
    00147       /**
    00148        *  Buffer for external characters. Used for input when
    00149        *  codecvt::always_noconv() == false. When valid, this corresponds
    00150        *  to eback().
    00151       */
    00152       char*         _M_ext_buf;
    00153
    00154       /**
    00155        *  Size of buffer held by _M_ext_buf.
    00156       */
    00157       streamsize        _M_ext_buf_size;
    00158
    00159       /**
    00160        *  Pointers into the buffer held by _M_ext_buf that delimit a
    00161        *  subsequence of bytes that have been read but not yet converted.
    00162        *  When valid, _M_ext_next corresponds to egptr().
    00163       */
    00164       const char*       _M_ext_next;
    00165       char*         _M_ext_end;
    00166
    00167       /**
    00168        *  Initializes pback buffers, and moves normal buffers to safety.
    00169        *  Assumptions:
    00170        *  _M_in_cur has already been moved back
    00171       */
    00172       void
    00173       _M_create_pback()
    00174       {
    00175     if (!_M_pback_init)
    00176       {
    00177         _M_pback_cur_save = this->gptr();
    00178         _M_pback_end_save = this->egptr();
    00179         this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
    00180         _M_pback_init = true;
    00181       }
    00182       }
    00183
    00184       /**
    00185        *  Deactivates pback buffer contents, and restores normal buffer.
    00186        *  Assumptions:
    00187        *  The pback buffer has only moved forward.
    00188       */
    00189       void
    00190       _M_destroy_pback() throw()
    00191       {
    00192     if (_M_pback_init)
    00193       {
    00194         // Length _M_in_cur moved in the pback buffer.
    00195         _M_pback_cur_save += this->gptr() != this->eback();
    00196         this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
    00197         _M_pback_init = false;
    00198       }
    00199       }
    00200
    00201     public:
    00202       // Constructors/destructor:
    00203       /**
    00204        *  @brief  Does not open any files.
    00205        *
    00206        *  The default constructor initializes the parent class using its
    00207        *  own default ctor.
    00208       */
    00209       basic_filebuf();
    00210
    00211       /**
    00212        *  @brief  The destructor closes the file first.
    00213       */
    00214       virtual
    00215       ~basic_filebuf()
    00216       { this->close(); }
    00217
    00218       // Members:
    00219       /**
    00220        *  @brief  Returns true if the external file is open.
    00221       */
    00222       bool
    00223       is_open() const throw()
    00224       { return _M_file.is_open(); }
    00225
    00226       /**
    00227        *  @brief  Opens an external file.
    00228        *  @param  s  The name of the file.
    00229        *  @param  mode  The open mode flags.
    00230        *  @return  @c this on success, NULL on failure
    00231        *
    00232        *  If a file is already open, this function immediately fails.
    00233        *  Otherwise it tries to open the file named @a s using the flags
    00234        *  given in @a mode.
    00235        *
    00236        *  Table 92, adapted here, gives the relation between openmode
    00237        *  combinations and the equivalent fopen() flags.
    00238        *  (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,
    00239        *  and binary|in|app per DR 596)
    00240        *  +---------------------------------------------------------+
    00241        *  | ios_base Flag combination            stdio equivalent   |
    00242        *  |binary  in  out  trunc  app                              |
    00243        *  +---------------------------------------------------------+
    00244        *  |             +                        "w"                |
    00245        *  |             +           +            "a"                |
    00246        *  |                         +            "a"                |
    00247        *  |             +     +                  "w"                |
    00248        *  |         +                            "r"                |
    00249        *  |         +   +                        "r+"               |
    00250        *  |         +   +     +                  "w+"               |
    00251        *  |         +   +           +            "a+"               |
    00252        *  |         +               +            "a+"               |
    00253        *  +---------------------------------------------------------+
    00254        *  |   +         +                        "wb"               |
    00255        *  |   +         +           +            "ab"               |
    00256        *  |   +                     +            "ab"               |
    00257        *  |   +         +     +                  "wb"               |
    00258        *  |   +     +                            "rb"               |
    00259        *  |   +     +   +                        "r+b"              |
    00260        *  |   +     +   +     +                  "w+b"              |
    00261        *  |   +     +   +           +            "a+b"              |
    00262        *  |   +     +               +            "a+b"              |
    00263        *  +---------------------------------------------------------+
    00264        */
    00265       __filebuf_type*
    00266       open(const char* __s, ios_base::openmode __mode);
    00267
    00268       /**
    00269        *  @brief  Closes the currently associated file.
    00270        *  @return  @c this on success, NULL on failure
    00271        *
    00272        *  If no file is currently open, this function immediately fails.
    00273        *
    00274        *  If a "put buffer area" exists, @c overflow(eof) is called to flush
    00275        *  all the characters.  The file is then closed.
    00276        *
    00277        *  If any operations fail, this function also fails.
    00278       */
    00279       __filebuf_type*
    00280       close();
    00281
    00282     protected:
    00283       void
    00284       _M_allocate_internal_buffer();
    00285
    00286       void
    00287       _M_destroy_internal_buffer() throw();
    00288
    00289       // [27.8.1.4] overridden virtual functions
    00290       virtual streamsize
    00291       showmanyc();
    00292
    00293       // Stroustrup, 1998, p. 628
    00294       // underflow() and uflow() functions are called to get the next
    00295       // character from the real input source when the buffer is empty.
    00296       // Buffered input uses underflow()
    00297
    00298       virtual int_type
    00299       underflow();
    00300
    00301       virtual int_type
    00302       pbackfail(int_type __c = _Traits::eof());
    00303
    00304       // Stroustrup, 1998, p 648
    00305       // The overflow() function is called to transfer characters to the
    00306       // real output destination when the buffer is full. A call to
    00307       // overflow(c) outputs the contents of the buffer plus the
    00308       // character c.
    00309       // 27.5.2.4.5
    00310       // Consume some sequence of the characters in the pending sequence.
    00311       virtual int_type
    00312       overflow(int_type __c = _Traits::eof());
    00313
    00314       // Convert internal byte sequence to external, char-based
    00315       // sequence via codecvt.
    00316       bool
    00317       _M_convert_to_external(char_type*, streamsize);
    00318
    00319       /**
    00320        *  @brief  Manipulates the buffer.
    00321        *  @param  s  Pointer to a buffer area.
    00322        *  @param  n  Size of @a s.
    00323        *  @return  @c this
    00324        *
    00325        *  If no file has been opened, and both @a s and @a n are zero, then
    00326        *  the stream becomes unbuffered.  Otherwise, @c s is used as a
    00327        *  buffer; see
    00328        *  http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
    00329        *  for more.
    00330       */
    00331       virtual __streambuf_type*
    00332       setbuf(char_type* __s, streamsize __n);
    00333
    00334       virtual pos_type
    00335       seekoff(off_type __off, ios_base::seekdir __way,
    00336           ios_base::openmode __mode = ios_base::in | ios_base::out);
    00337
    00338       virtual pos_type
    00339       seekpos(pos_type __pos,
    00340           ios_base::openmode __mode = ios_base::in | ios_base::out);
    00341
    00342       // Common code for seekoff and seekpos
    00343       pos_type
    00344       _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
    00345
    00346       virtual int
    00347       sync();
    00348
    00349       virtual void
    00350       imbue(const locale& __loc);
    00351
    00352       virtual streamsize
    00353       xsgetn(char_type* __s, streamsize __n);
    00354
    00355       virtual streamsize
    00356       xsputn(const char_type* __s, streamsize __n);
    00357
    00358       // Flushes output buffer, then writes unshift sequence.
    00359       bool
    00360       _M_terminate_output();
    00361
    00362       /**
    00363        *  This function sets the pointers of the internal buffer, both get
    00364        *  and put areas. Typically:
    00365        *
    00366        *   __off == egptr() - eback() upon underflow/uflow ('read' mode);
    00367        *   __off == 0 upon overflow ('write' mode);
    00368        *   __off == -1 upon open, setbuf, seekoff/pos ('uncommitted' mode).
    00369        *
    00370        *  NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
    00371        *  reflects the actual allocated memory and the last cell is reserved
    00372        *  for the overflow char of a full put area.
    00373       */
    00374       void
    00375       _M_set_buffer(streamsize __off)
    00376       {
    00377     const bool __testin = _M_mode & ios_base::in;
    00378     const bool __testout = _M_mode & ios_base::out;
    00379
    00380     if (__testin && __off > 0)
    00381       this->setg(_M_buf, _M_buf, _M_buf + __off);
    00382     else
    00383       this->setg(_M_buf, _M_buf, _M_buf);
    00384
    00385     if (__testout && __off == 0 && _M_buf_size > 1 )
    00386       this->setp(_M_buf, _M_buf + _M_buf_size - 1);
    00387     else
    00388       this->setp(NULL, NULL);
    00389       }
    00390     };
    00391
    00392   // [27.8.1.5] Template class basic_ifstream
    00393   /**
    00394    *  @brief  Controlling input for files.
    00395    *
    00396    *  This class supports reading from named files, using the inherited
    00397    *  functions from std::basic_istream.  To control the associated
    00398    *  sequence, an instance of std::basic_filebuf is used, which this page
    00399    *  refers to as @c sb.
    00400   */
    00401   template<typename _CharT, typename _Traits>
    00402     class basic_ifstream : public basic_istream<_CharT, _Traits>
    00403     {
    00404     public:
    00405       // Types:
    00406       typedef _CharT                    char_type;
    00407       typedef _Traits                   traits_type;
    00408       typedef typename traits_type::int_type        int_type;
    00409       typedef typename traits_type::pos_type        pos_type;
    00410       typedef typename traits_type::off_type        off_type;
    00411
    00412       // Non-standard types:
    00413       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
    00414       typedef basic_istream<char_type, traits_type> __istream_type;
    00415
    00416     private:
    00417       __filebuf_type    _M_filebuf;
    00418
    00419     public:
    00420       // Constructors/Destructors:
    00421       /**
    00422        *  @brief  Default constructor.
    00423        *
    00424        *  Initializes @c sb using its default constructor, and passes
    00425        *  @c &sb to the base class initializer.  Does not open any files
    00426        *  (you haven't given it a filename to open).
    00427       */
    00428       basic_ifstream() : __istream_type(), _M_filebuf()
    00429       { this->init(&_M_filebuf); }
    00430
    00431       /**
    00432        *  @brief  Create an input file stream.
    00433        *  @param  s  Null terminated string specifying the filename.
    00434        *  @param  mode  Open file in specified mode (see std::ios_base).
    00435        *
    00436        *  @c ios_base::in is automatically included in @a mode.
    00437        *
    00438        *  Tip:  When using std::string to hold the filename, you must use
    00439        *  .c_str() before passing it to this constructor.
    00440       */
    00441       explicit
    00442       basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
    00443       : __istream_type(), _M_filebuf()
    00444       {
    00445     this->init(&_M_filebuf);
    00446     this->open(__s, __mode);
    00447       }
    00448
    00449       /**
    00450        *  @brief  The destructor does nothing.
    00451        *
    00452        *  The file is closed by the filebuf object, not the formatting
    00453        *  stream.
    00454       */
    00455       ~basic_ifstream()
    00456       { }
    00457
    00458       // Members:
    00459       /**
    00460        *  @brief  Accessing the underlying buffer.
    00461        *  @return  The current basic_filebuf buffer.
    00462        *
    00463        *  This hides both signatures of std::basic_ios::rdbuf().
    00464       */
    00465       __filebuf_type*
    00466       rdbuf() const
    00467       { return const_cast<__filebuf_type*>(&_M_filebuf); }
    00468
    00469       /**
    00470        *  @brief  Wrapper to test for an open file.
    00471        *  @return  @c rdbuf()->is_open()
    00472       */
    00473       bool
    00474       is_open()
    00475       { return _M_filebuf.is_open(); }
    00476
    00477       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00478       // 365. Lack of const-qualification in clause 27
    00479       bool
    00480       is_open() const
    00481       { return _M_filebuf.is_open(); }
    00482
    00483       /**
    00484        *  @brief  Opens an external file.
    00485        *  @param  s  The name of the file.
    00486        *  @param  mode  The open mode flags.
    00487        *
    00488        *  Calls @c std::basic_filebuf::open(s,mode|in).  If that function
    00489        *  fails, @c failbit is set in the stream's error state.
    00490        *
    00491        *  Tip:  When using std::string to hold the filename, you must use
    00492        *  .c_str() before passing it to this constructor.
    00493       */
    00494       void
    00495       open(const char* __s, ios_base::openmode __mode = ios_base::in)
    00496       {
    00497     if (!_M_filebuf.open(__s, __mode | ios_base::in))
    00498       this->setstate(ios_base::failbit);
    00499     else
    00500       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00501       // 409. Closing an fstream should clear error state
    00502       this->clear();
    00503       }
    00504
    00505       /**
    00506        *  @brief  Close the file.
    00507        *
    00508        *  Calls @c std::basic_filebuf::close().  If that function
    00509        *  fails, @c failbit is set in the stream's error state.
    00510       */
    00511       void
    00512       close()
    00513       {
    00514     if (!_M_filebuf.close())
    00515       this->setstate(ios_base::failbit);
    00516       }
    00517     };
    00518
    00519
    00520   // [27.8.1.8] Template class basic_ofstream
    00521   /**
    00522    *  @brief  Controlling output for files.
    00523    *
    00524    *  This class supports reading from named files, using the inherited
    00525    *  functions from std::basic_ostream.  To control the associated
    00526    *  sequence, an instance of std::basic_filebuf is used, which this page
    00527    *  refers to as @c sb.
    00528   */
    00529   template<typename _CharT, typename _Traits>
    00530     class basic_ofstream : public basic_ostream<_CharT,_Traits>
    00531     {
    00532     public:
    00533       // Types:
    00534       typedef _CharT                    char_type;
    00535       typedef _Traits                   traits_type;
    00536       typedef typename traits_type::int_type        int_type;
    00537       typedef typename traits_type::pos_type        pos_type;
    00538       typedef typename traits_type::off_type        off_type;
    00539
    00540       // Non-standard types:
    00541       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
    00542       typedef basic_ostream<char_type, traits_type> __ostream_type;
    00543
    00544     private:
    00545       __filebuf_type    _M_filebuf;
    00546
    00547     public:
    00548       // Constructors:
    00549       /**
    00550        *  @brief  Default constructor.
    00551        *
    00552        *  Initializes @c sb using its default constructor, and passes
    00553        *  @c &sb to the base class initializer.  Does not open any files
    00554        *  (you haven't given it a filename to open).
    00555       */
    00556       basic_ofstream(): __ostream_type(), _M_filebuf()
    00557       { this->init(&_M_filebuf); }
    00558
    00559       /**
    00560        *  @brief  Create an output file stream.
    00561        *  @param  s  Null terminated string specifying the filename.
    00562        *  @param  mode  Open file in specified mode (see std::ios_base).
    00563        *
    00564        *  @c ios_base::out|ios_base::trunc is automatically included in
    00565        *  @a mode.
    00566        *
    00567        *  Tip:  When using std::string to hold the filename, you must use
    00568        *  .c_str() before passing it to this constructor.
    00569       */
    00570       explicit
    00571       basic_ofstream(const char* __s,
    00572              ios_base::openmode __mode = ios_base::out|ios_base::trunc)
    00573       : __ostream_type(), _M_filebuf()
    00574       {
    00575     this->init(&_M_filebuf);
    00576     this->open(__s, __mode);
    00577       }
    00578
    00579       /**
    00580        *  @brief  The destructor does nothing.
    00581        *
    00582        *  The file is closed by the filebuf object, not the formatting
    00583        *  stream.
    00584       */
    00585       ~basic_ofstream()
    00586       { }
    00587
    00588       // Members:
    00589       /**
    00590        *  @brief  Accessing the underlying buffer.
    00591        *  @return  The current basic_filebuf buffer.
    00592        *
    00593        *  This hides both signatures of std::basic_ios::rdbuf().
    00594       */
    00595       __filebuf_type*
    00596       rdbuf() const
    00597       { return const_cast<__filebuf_type*>(&_M_filebuf); }
    00598
    00599       /**
    00600        *  @brief  Wrapper to test for an open file.
    00601        *  @return  @c rdbuf()->is_open()
    00602       */
    00603       bool
    00604       is_open()
    00605       { return _M_filebuf.is_open(); }
    00606
    00607       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00608       // 365. Lack of const-qualification in clause 27
    00609       bool
    00610       is_open() const
    00611       { return _M_filebuf.is_open(); }
    00612
    00613       /**
    00614        *  @brief  Opens an external file.
    00615        *  @param  s  The name of the file.
    00616        *  @param  mode  The open mode flags.
    00617        *
    00618        *  Calls @c std::basic_filebuf::open(s,mode|out|trunc).  If that
    00619        *  function fails, @c failbit is set in the stream's error state.
    00620        *
    00621        *  Tip:  When using std::string to hold the filename, you must use
    00622        *  .c_str() before passing it to this constructor.
    00623       */
    00624       void
    00625       open(const char* __s,
    00626        ios_base::openmode __mode = ios_base::out | ios_base::trunc)
    00627       {
    00628     if (!_M_filebuf.open(__s, __mode | ios_base::out))
    00629       this->setstate(ios_base::failbit);
    00630     else
    00631       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00632       // 409. Closing an fstream should clear error state
    00633       this->clear();
    00634       }
    00635
    00636       /**
    00637        *  @brief  Close the file.
    00638        *
    00639        *  Calls @c std::basic_filebuf::close().  If that function
    00640        *  fails, @c failbit is set in the stream's error state.
    00641       */
    00642       void
    00643       close()
    00644       {
    00645     if (!_M_filebuf.close())
    00646       this->setstate(ios_base::failbit);
    00647       }
    00648     };
    00649
    00650
    00651   // [27.8.1.11] Template class basic_fstream
    00652   /**
    00653    *  @brief  Controlling input and output for files.
    00654    *
    00655    *  This class supports reading from and writing to named files, using
    00656    *  the inherited functions from std::basic_iostream.  To control the
    00657    *  associated sequence, an instance of std::basic_filebuf is used, which
    00658    *  this page refers to as @c sb.
    00659   */
    00660   template<typename _CharT, typename _Traits>
    00661     class basic_fstream : public basic_iostream<_CharT, _Traits>
    00662     {
    00663     public:
    00664       // Types:
    00665       typedef _CharT                    char_type;
    00666       typedef _Traits                   traits_type;
    00667       typedef typename traits_type::int_type        int_type;
    00668       typedef typename traits_type::pos_type        pos_type;
    00669       typedef typename traits_type::off_type        off_type;
    00670
    00671       // Non-standard types:
    00672       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
    00673       typedef basic_ios<char_type, traits_type>     __ios_type;
    00674       typedef basic_iostream<char_type, traits_type>    __iostream_type;
    00675
    00676     private:
    00677       __filebuf_type    _M_filebuf;
    00678
    00679     public:
    00680       // Constructors/destructor:
    00681       /**
    00682        *  @brief  Default constructor.
    00683        *
    00684        *  Initializes @c sb using its default constructor, and passes
    00685        *  @c &sb to the base class initializer.  Does not open any files
    00686        *  (you haven't given it a filename to open).
    00687       */
    00688       basic_fstream()
    00689       : __iostream_type(), _M_filebuf()
    00690       { this->init(&_M_filebuf); }
    00691
    00692       /**
    00693        *  @brief  Create an input/output file stream.
    00694        *  @param  s  Null terminated string specifying the filename.
    00695        *  @param  mode  Open file in specified mode (see std::ios_base).
    00696        *
    00697        *  Tip:  When using std::string to hold the filename, you must use
    00698        *  .c_str() before passing it to this constructor.
    00699       */
    00700       explicit
    00701       basic_fstream(const char* __s,
    00702             ios_base::openmode __mode = ios_base::in | ios_base::out)
    00703       : __iostream_type(NULL), _M_filebuf()
    00704       {
    00705     this->init(&_M_filebuf);
    00706     this->open(__s, __mode);
    00707       }
    00708
    00709       /**
    00710        *  @brief  The destructor does nothing.
    00711        *
    00712        *  The file is closed by the filebuf object, not the formatting
    00713        *  stream.
    00714       */
    00715       ~basic_fstream()
    00716       { }
    00717
    00718       // Members:
    00719       /**
    00720        *  @brief  Accessing the underlying buffer.
    00721        *  @return  The current basic_filebuf buffer.
    00722        *
    00723        *  This hides both signatures of std::basic_ios::rdbuf().
    00724       */
    00725       __filebuf_type*
    00726       rdbuf() const
    00727       { return const_cast<__filebuf_type*>(&_M_filebuf); }
    00728
    00729       /**
    00730        *  @brief  Wrapper to test for an open file.
    00731        *  @return  @c rdbuf()->is_open()
    00732       */
    00733       bool
    00734       is_open()
    00735       { return _M_filebuf.is_open(); }
    00736
    00737       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00738       // 365. Lack of const-qualification in clause 27
    00739       bool
    00740       is_open() const
    00741       { return _M_filebuf.is_open(); }
    00742
    00743       /**
    00744        *  @brief  Opens an external file.
    00745        *  @param  s  The name of the file.
    00746        *  @param  mode  The open mode flags.
    00747        *
    00748        *  Calls @c std::basic_filebuf::open(s,mode).  If that
    00749        *  function fails, @c failbit is set in the stream's error state.
    00750        *
    00751        *  Tip:  When using std::string to hold the filename, you must use
    00752        *  .c_str() before passing it to this constructor.
    00753       */
    00754       void
    00755       open(const char* __s,
    00756        ios_base::openmode __mode = ios_base::in | ios_base::out)
    00757       {
    00758     if (!_M_filebuf.open(__s, __mode))
    00759       this->setstate(ios_base::failbit);
    00760     else
    00761       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00762       // 409. Closing an fstream should clear error state
    00763       this->clear();
    00764       }
    00765
    00766       /**
    00767        *  @brief  Close the file.
    00768        *
    00769        *  Calls @c std::basic_filebuf::close().  If that function
    00770        *  fails, @c failbit is set in the stream's error state.
    00771       */
    00772       void
    00773       close()
    00774       {
    00775     if (!_M_filebuf.close())
    00776       this->setstate(ios_base::failbit);
    00777       }
    00778     };
    00779
    00780 _GLIBCXX_END_NAMESPACE
    00781
    00782 #ifndef _GLIBCXX_EXPORT_TEMPLATE
    00783 # include <bits/fstream.tcc>
    00784 #endif
    00785
    00786 #endif /* _GLIBCXX_FSTREAM */