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