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

Generated on Thu Jul 23 21:16:06 2009 for libstdc++ by  doxygen 1.5.8