libstdc++
fstream
Go to the documentation of this file.
00001 // File based streams -*- C++ -*-
00002 
00003 // Copyright (C) 1997-2018 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file include/fstream
00026  *  This is a Standard C++ Library header.
00027  */
00028 
00029 //
00030 // ISO C++ 14882: 27.8  File-based streams
00031 //
00032 
00033 #ifndef _GLIBCXX_FSTREAM
00034 #define _GLIBCXX_FSTREAM 1
00035 
00036 #pragma GCC system_header
00037 
00038 #include <istream>
00039 #include <ostream>
00040 #include <bits/codecvt.h>
00041 #include <cstdio>             // For BUFSIZ
00042 #include <bits/basic_file.h>  // For __basic_file, __c_lock
00043 #if __cplusplus >= 201103L
00044 #include <string>             // For std::string overloads.
00045 #endif
00046 
00047 namespace std _GLIBCXX_VISIBILITY(default)
00048 {
00049 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00050 
00051 #if __cplusplus >= 201703L
00052   // Enable if _Path is a filesystem::path or experimental::filesystem::path
00053   template<typename _Path, typename _Result = _Path, typename _Path2
00054            = decltype(std::declval<_Path&>().make_preferred().filename())>
00055     using _If_fs_path = enable_if_t<is_same_v<_Path, _Path2>, _Result>;
00056 #endif // C++17
00057 
00058 
00059   // [27.8.1.1] template class basic_filebuf
00060   /**
00061    *  @brief  The actual work of input and output (for files).
00062    *  @ingroup io
00063    *
00064    *  @tparam _CharT  Type of character stream.
00065    *  @tparam _Traits  Traits for character type, defaults to
00066    *                   char_traits<_CharT>.
00067    *
00068    *  This class associates both its input and output sequence with an
00069    *  external disk file, and maintains a joint file position for both
00070    *  sequences.  Many of its semantics are described in terms of similar
00071    *  behavior in the Standard C Library's @c FILE streams.
00072    *
00073    *  Requirements on traits_type, specific to this class:
00074    *  - traits_type::pos_type must be fpos<traits_type::state_type>
00075    *  - traits_type::off_type must be streamoff
00076    *  - traits_type::state_type must be Assignable and DefaultConstructible,
00077    *  - traits_type::state_type() must be the initial state for codecvt.
00078    */
00079   template<typename _CharT, typename _Traits>
00080     class basic_filebuf : public basic_streambuf<_CharT, _Traits>
00081     {
00082 #if __cplusplus >= 201103L
00083       template<typename _Tp>
00084         using __chk_state = __and_<is_copy_assignable<_Tp>,
00085                                    is_copy_constructible<_Tp>,
00086                                    is_default_constructible<_Tp>>;
00087 
00088       static_assert(__chk_state<typename _Traits::state_type>::value,
00089                     "state_type must be CopyAssignable, CopyConstructible"
00090                     " and DefaultConstructible");
00091 
00092       static_assert(is_same<typename _Traits::pos_type,
00093                             fpos<typename _Traits::state_type>>::value,
00094                     "pos_type must be fpos<state_type>");
00095 #endif
00096     public:
00097       // Types:
00098       typedef _CharT                                    char_type;
00099       typedef _Traits                                   traits_type;
00100       typedef typename traits_type::int_type            int_type;
00101       typedef typename traits_type::pos_type            pos_type;
00102       typedef typename traits_type::off_type            off_type;
00103 
00104       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
00105       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00106       typedef __basic_file<char>                        __file_type;
00107       typedef typename traits_type::state_type          __state_type;
00108       typedef codecvt<char_type, char, __state_type>    __codecvt_type;
00109 
00110       friend class ios_base; // For sync_with_stdio.
00111 
00112     protected:
00113       // Data Members:
00114       // MT lock inherited from libio or other low-level io library.
00115       __c_lock                  _M_lock;
00116 
00117       // External buffer.
00118       __file_type               _M_file;
00119 
00120       /// Place to stash in || out || in | out settings for current filebuf.
00121       ios_base::openmode        _M_mode;
00122 
00123       // Beginning state type for codecvt.
00124       __state_type              _M_state_beg;
00125 
00126       // During output, the state that corresponds to pptr(),
00127       // during input, the state that corresponds to egptr() and
00128       // _M_ext_next.
00129       __state_type              _M_state_cur;
00130 
00131       // Not used for output. During input, the state that corresponds
00132       // to eback() and _M_ext_buf.
00133       __state_type              _M_state_last;
00134 
00135       /// Pointer to the beginning of internal buffer.
00136       char_type*                _M_buf;
00137 
00138       /**
00139        *  Actual size of internal buffer. This number is equal to the size
00140        *  of the put area + 1 position, reserved for the overflow char of
00141        *  a full area.
00142        */
00143       size_t                    _M_buf_size;
00144 
00145       // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
00146       bool                      _M_buf_allocated;
00147 
00148       /**
00149        *  _M_reading == false && _M_writing == false for @b uncommitted mode;
00150        *  _M_reading == true for @b read mode;
00151        *  _M_writing == true for @b write mode;
00152        *
00153        *  NB: _M_reading == true && _M_writing == true is unused.
00154        */
00155       bool                      _M_reading;
00156       bool                      _M_writing;
00157 
00158       //@{
00159       /**
00160        *  Necessary bits for putback buffer management.
00161        *
00162        *  @note pbacks of over one character are not currently supported.
00163        */
00164       char_type                 _M_pback;
00165       char_type*                _M_pback_cur_save;
00166       char_type*                _M_pback_end_save;
00167       bool                      _M_pback_init;
00168       //@}
00169 
00170       // Cached codecvt facet.
00171       const __codecvt_type*     _M_codecvt;
00172 
00173       /**
00174        *  Buffer for external characters. Used for input when
00175        *  codecvt::always_noconv() == false. When valid, this corresponds
00176        *  to eback().
00177        */
00178       char*                     _M_ext_buf;
00179 
00180       /**
00181        *  Size of buffer held by _M_ext_buf.
00182        */
00183       streamsize                _M_ext_buf_size;
00184 
00185       /**
00186        *  Pointers into the buffer held by _M_ext_buf that delimit a
00187        *  subsequence of bytes that have been read but not yet converted.
00188        *  When valid, _M_ext_next corresponds to egptr().
00189        */
00190       const char*               _M_ext_next;
00191       char*                     _M_ext_end;
00192 
00193       /**
00194        *  Initializes pback buffers, and moves normal buffers to safety.
00195        *  Assumptions:
00196        *  _M_in_cur has already been moved back
00197        */
00198       void
00199       _M_create_pback()
00200       {
00201         if (!_M_pback_init)
00202           {
00203             _M_pback_cur_save = this->gptr();
00204             _M_pback_end_save = this->egptr();
00205             this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
00206             _M_pback_init = true;
00207           }
00208       }
00209 
00210       /**
00211        *  Deactivates pback buffer contents, and restores normal buffer.
00212        *  Assumptions:
00213        *  The pback buffer has only moved forward.
00214        */
00215       void
00216       _M_destroy_pback() throw()
00217       {
00218         if (_M_pback_init)
00219           {
00220             // Length _M_in_cur moved in the pback buffer.
00221             _M_pback_cur_save += this->gptr() != this->eback();
00222             this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
00223             _M_pback_init = false;
00224           }
00225       }
00226 
00227     public:
00228       // Constructors/destructor:
00229       /**
00230        *  @brief  Does not open any files.
00231        *
00232        *  The default constructor initializes the parent class using its
00233        *  own default ctor.
00234        */
00235       basic_filebuf();
00236 
00237 #if __cplusplus >= 201103L
00238       basic_filebuf(const basic_filebuf&) = delete;
00239       basic_filebuf(basic_filebuf&&);
00240 #endif
00241 
00242       /**
00243        *  @brief  The destructor closes the file first.
00244        */
00245       virtual
00246       ~basic_filebuf()
00247       { this->close(); }
00248 
00249 #if __cplusplus >= 201103L
00250       basic_filebuf& operator=(const basic_filebuf&) = delete;
00251       basic_filebuf& operator=(basic_filebuf&&);
00252       void swap(basic_filebuf&);
00253 #endif
00254 
00255       // Members:
00256       /**
00257        *  @brief  Returns true if the external file is open.
00258        */
00259       bool
00260       is_open() const throw()
00261       { return _M_file.is_open(); }
00262 
00263       /**
00264        *  @brief  Opens an external file.
00265        *  @param  __s  The name of the file.
00266        *  @param  __mode  The open mode flags.
00267        *  @return  @c this on success, NULL on failure
00268        *
00269        *  If a file is already open, this function immediately fails.
00270        *  Otherwise it tries to open the file named @a __s using the flags
00271        *  given in @a __mode.
00272        *
00273        *  Table 92, adapted here, gives the relation between openmode
00274        *  combinations and the equivalent @c fopen() flags.
00275        *  (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,
00276        *  and binary|in|app per DR 596)
00277        *  <pre>
00278        *  +---------------------------------------------------------+
00279        *  | ios_base Flag combination            stdio equivalent   |
00280        *  |binary  in  out  trunc  app                              |
00281        *  +---------------------------------------------------------+
00282        *  |             +                        w                  |
00283        *  |             +           +            a                  |
00284        *  |                         +            a                  |
00285        *  |             +     +                  w                  |
00286        *  |         +                            r                  |
00287        *  |         +   +                        r+                 |
00288        *  |         +   +     +                  w+                 |
00289        *  |         +   +           +            a+                 |
00290        *  |         +               +            a+                 |
00291        *  +---------------------------------------------------------+
00292        *  |   +         +                        wb                 |
00293        *  |   +         +           +            ab                 |
00294        *  |   +                     +            ab                 |
00295        *  |   +         +     +                  wb                 |
00296        *  |   +     +                            rb                 |
00297        *  |   +     +   +                        r+b                |
00298        *  |   +     +   +     +                  w+b                |
00299        *  |   +     +   +           +            a+b                |
00300        *  |   +     +               +            a+b                |
00301        *  +---------------------------------------------------------+
00302        *  </pre>
00303        */
00304       __filebuf_type*
00305       open(const char* __s, ios_base::openmode __mode);
00306 
00307 #if __cplusplus >= 201103L
00308       /**
00309        *  @brief  Opens an external file.
00310        *  @param  __s  The name of the file.
00311        *  @param  __mode  The open mode flags.
00312        *  @return  @c this on success, NULL on failure
00313        */
00314       __filebuf_type*
00315       open(const std::string& __s, ios_base::openmode __mode)
00316       { return open(__s.c_str(), __mode); }
00317 
00318 #if __cplusplus >= 201703L
00319       /**
00320        *  @brief  Opens an external file.
00321        *  @param  __s  The name of the file, as a filesystem::path.
00322        *  @param  __mode  The open mode flags.
00323        *  @return  @c this on success, NULL on failure
00324        */
00325       template<typename _Path>
00326         _If_fs_path<_Path, __filebuf_type*>
00327         open(const _Path& __s, ios_base::openmode __mode)
00328         { return open(__s.c_str(), __mode); }
00329 #endif // C++17
00330 #endif // C++11
00331 
00332       /**
00333        *  @brief  Closes the currently associated file.
00334        *  @return  @c this on success, NULL on failure
00335        *
00336        *  If no file is currently open, this function immediately fails.
00337        *
00338        *  If a <em>put buffer area</em> exists, @c overflow(eof) is
00339        *  called to flush all the characters.  The file is then
00340        *  closed.
00341        *
00342        *  If any operations fail, this function also fails.
00343        */
00344       __filebuf_type*
00345       close();
00346 
00347     protected:
00348       void
00349       _M_allocate_internal_buffer();
00350 
00351       void
00352       _M_destroy_internal_buffer() throw();
00353 
00354       // [27.8.1.4] overridden virtual functions
00355       virtual streamsize
00356       showmanyc();
00357 
00358       // Stroustrup, 1998, p. 628
00359       // underflow() and uflow() functions are called to get the next
00360       // character from the real input source when the buffer is empty.
00361       // Buffered input uses underflow()
00362 
00363       virtual int_type
00364       underflow();
00365 
00366       virtual int_type
00367       pbackfail(int_type __c = _Traits::eof());
00368 
00369       // Stroustrup, 1998, p 648
00370       // The overflow() function is called to transfer characters to the
00371       // real output destination when the buffer is full. A call to
00372       // overflow(c) outputs the contents of the buffer plus the
00373       // character c.
00374       // 27.5.2.4.5
00375       // Consume some sequence of the characters in the pending sequence.
00376       virtual int_type
00377       overflow(int_type __c = _Traits::eof());
00378 
00379       // Convert internal byte sequence to external, char-based
00380       // sequence via codecvt.
00381       bool
00382       _M_convert_to_external(char_type*, streamsize);
00383 
00384       /**
00385        *  @brief  Manipulates the buffer.
00386        *  @param  __s  Pointer to a buffer area.
00387        *  @param  __n  Size of @a __s.
00388        *  @return  @c this
00389        *
00390        *  If no file has been opened, and both @a __s and @a __n are zero, then
00391        *  the stream becomes unbuffered.  Otherwise, @c __s is used as a
00392        *  buffer; see
00393        *  https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
00394        *  for more.
00395        */
00396       virtual __streambuf_type*
00397       setbuf(char_type* __s, streamsize __n);
00398 
00399       virtual pos_type
00400       seekoff(off_type __off, ios_base::seekdir __way,
00401               ios_base::openmode __mode = ios_base::in | ios_base::out);
00402 
00403       virtual pos_type
00404       seekpos(pos_type __pos,
00405               ios_base::openmode __mode = ios_base::in | ios_base::out);
00406 
00407       // Common code for seekoff, seekpos, and overflow
00408       pos_type
00409       _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
00410 
00411       int
00412       _M_get_ext_pos(__state_type &__state);
00413 
00414       virtual int
00415       sync();
00416 
00417       virtual void
00418       imbue(const locale& __loc);
00419 
00420       virtual streamsize
00421       xsgetn(char_type* __s, streamsize __n);
00422 
00423       virtual streamsize
00424       xsputn(const char_type* __s, streamsize __n);
00425 
00426       // Flushes output buffer, then writes unshift sequence.
00427       bool
00428       _M_terminate_output();
00429 
00430       /**
00431        *  This function sets the pointers of the internal buffer, both get
00432        *  and put areas. Typically:
00433        *
00434        *   __off == egptr() - eback() upon underflow/uflow (@b read mode);
00435        *   __off == 0 upon overflow (@b write mode);
00436        *   __off == -1 upon open, setbuf, seekoff/pos (@b uncommitted mode).
00437        *
00438        *  NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
00439        *  reflects the actual allocated memory and the last cell is reserved
00440        *  for the overflow char of a full put area.
00441        */
00442       void
00443       _M_set_buffer(streamsize __off)
00444       {
00445         const bool __testin = _M_mode & ios_base::in;
00446         const bool __testout = (_M_mode & ios_base::out
00447                                 || _M_mode & ios_base::app);
00448 
00449         if (__testin && __off > 0)
00450           this->setg(_M_buf, _M_buf, _M_buf + __off);
00451         else
00452           this->setg(_M_buf, _M_buf, _M_buf);
00453 
00454         if (__testout && __off == 0 && _M_buf_size > 1 )
00455           this->setp(_M_buf, _M_buf + _M_buf_size - 1);
00456         else
00457           this->setp(0, 0);
00458       }
00459     };
00460 
00461   // [27.8.1.5] Template class basic_ifstream
00462   /**
00463    *  @brief  Controlling input for files.
00464    *  @ingroup io
00465    *
00466    *  @tparam _CharT  Type of character stream.
00467    *  @tparam _Traits  Traits for character type, defaults to
00468    *                   char_traits<_CharT>.
00469    *
00470    *  This class supports reading from named files, using the inherited
00471    *  functions from std::basic_istream.  To control the associated
00472    *  sequence, an instance of std::basic_filebuf is used, which this page
00473    *  refers to as @c sb.
00474    */
00475   template<typename _CharT, typename _Traits>
00476     class basic_ifstream : public basic_istream<_CharT, _Traits>
00477     {
00478     public:
00479       // Types:
00480       typedef _CharT                                    char_type;
00481       typedef _Traits                                   traits_type;
00482       typedef typename traits_type::int_type            int_type;
00483       typedef typename traits_type::pos_type            pos_type;
00484       typedef typename traits_type::off_type            off_type;
00485 
00486       // Non-standard types:
00487       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00488       typedef basic_istream<char_type, traits_type>     __istream_type;
00489 
00490     private:
00491       __filebuf_type    _M_filebuf;
00492 
00493     public:
00494       // Constructors/Destructors:
00495       /**
00496        *  @brief  Default constructor.
00497        *
00498        *  Initializes @c sb using its default constructor, and passes
00499        *  @c &sb to the base class initializer.  Does not open any files
00500        *  (you haven't given it a filename to open).
00501        */
00502       basic_ifstream() : __istream_type(), _M_filebuf()
00503       { this->init(&_M_filebuf); }
00504 
00505       /**
00506        *  @brief  Create an input file stream.
00507        *  @param  __s  Null terminated string specifying the filename.
00508        *  @param  __mode  Open file in specified mode (see std::ios_base).
00509        *
00510        *  @c ios_base::in is automatically included in @a __mode.
00511        */
00512       explicit
00513       basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
00514       : __istream_type(), _M_filebuf()
00515       {
00516         this->init(&_M_filebuf);
00517         this->open(__s, __mode);
00518       }
00519 
00520 #if __cplusplus >= 201103L
00521       /**
00522        *  @brief  Create an input file stream.
00523        *  @param  __s  std::string specifying the filename.
00524        *  @param  __mode  Open file in specified mode (see std::ios_base).
00525        *
00526        *  @c ios_base::in is automatically included in @a __mode.
00527        */
00528       explicit
00529       basic_ifstream(const std::string& __s,
00530                      ios_base::openmode __mode = ios_base::in)
00531       : __istream_type(), _M_filebuf()
00532       {
00533         this->init(&_M_filebuf);
00534         this->open(__s, __mode);
00535       }
00536 
00537 #if __cplusplus >= 201703L
00538       /**
00539        *  @param  Create an input file stream.
00540        *  @param  __s  filesystem::path specifying the filename.
00541        *  @param  __mode  Open file in specified mode (see std::ios_base).
00542        *
00543        *  @c ios_base::in is automatically included in @a __mode.
00544        */
00545       template<typename _Path, typename _Require = _If_fs_path<_Path>>
00546         basic_ifstream(const _Path& __s,
00547                        ios_base::openmode __mode = ios_base::in)
00548         : basic_ifstream(__s.c_str(), __mode)
00549         { }
00550 #endif // C++17
00551 
00552       basic_ifstream(const basic_ifstream&) = delete;
00553 
00554       basic_ifstream(basic_ifstream&& __rhs)
00555       : __istream_type(std::move(__rhs)),
00556       _M_filebuf(std::move(__rhs._M_filebuf))
00557       { __istream_type::set_rdbuf(&_M_filebuf); }
00558 #endif // C++11
00559 
00560       /**
00561        *  @brief  The destructor does nothing.
00562        *
00563        *  The file is closed by the filebuf object, not the formatting
00564        *  stream.
00565        */
00566       ~basic_ifstream()
00567       { }
00568 
00569 #if __cplusplus >= 201103L
00570       // 27.8.3.2 Assign and swap:
00571 
00572       basic_ifstream&
00573       operator=(const basic_ifstream&) = delete;
00574 
00575       basic_ifstream&
00576       operator=(basic_ifstream&& __rhs)
00577       {
00578         __istream_type::operator=(std::move(__rhs));
00579         _M_filebuf = std::move(__rhs._M_filebuf);
00580         return *this;
00581       }
00582 
00583       void
00584       swap(basic_ifstream& __rhs)
00585       {
00586         __istream_type::swap(__rhs);
00587         _M_filebuf.swap(__rhs._M_filebuf);
00588       }
00589 #endif
00590 
00591       // Members:
00592       /**
00593        *  @brief  Accessing the underlying buffer.
00594        *  @return  The current basic_filebuf buffer.
00595        *
00596        *  This hides both signatures of std::basic_ios::rdbuf().
00597        */
00598       __filebuf_type*
00599       rdbuf() const
00600       { return const_cast<__filebuf_type*>(&_M_filebuf); }
00601 
00602       /**
00603        *  @brief  Wrapper to test for an open file.
00604        *  @return  @c rdbuf()->is_open()
00605        */
00606       bool
00607       is_open()
00608       { return _M_filebuf.is_open(); }
00609 
00610       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00611       // 365. Lack of const-qualification in clause 27
00612       bool
00613       is_open() const
00614       { return _M_filebuf.is_open(); }
00615 
00616       /**
00617        *  @brief  Opens an external file.
00618        *  @param  __s  The name of the file.
00619        *  @param  __mode  The open mode flags.
00620        *
00621        *  Calls @c std::basic_filebuf::open(s,__mode|in).  If that function
00622        *  fails, @c failbit is set in the stream's error state.
00623        */
00624       void
00625       open(const char* __s, ios_base::openmode __mode = ios_base::in)
00626       {
00627         if (!_M_filebuf.open(__s, __mode | ios_base::in))
00628           this->setstate(ios_base::failbit);
00629         else
00630           // _GLIBCXX_RESOLVE_LIB_DEFECTS
00631           // 409. Closing an fstream should clear error state
00632           this->clear();
00633       }
00634 
00635 #if __cplusplus >= 201103L
00636       /**
00637        *  @brief  Opens an external file.
00638        *  @param  __s  The name of the file.
00639        *  @param  __mode  The open mode flags.
00640        *
00641        *  Calls @c std::basic_filebuf::open(__s,__mode|in).  If that function
00642        *  fails, @c failbit is set in the stream's error state.
00643        */
00644       void
00645       open(const std::string& __s, ios_base::openmode __mode = ios_base::in)
00646       {
00647         if (!_M_filebuf.open(__s, __mode | ios_base::in))
00648           this->setstate(ios_base::failbit);
00649         else
00650           // _GLIBCXX_RESOLVE_LIB_DEFECTS
00651           // 409. Closing an fstream should clear error state
00652           this->clear();
00653       }
00654 
00655 #if __cplusplus >= 201703L
00656       /**
00657        *  @brief  Opens an external file.
00658        *  @param  __s  The name of the file, as a filesystem::path.
00659        *  @param  __mode  The open mode flags.
00660        *
00661        *  Calls @c std::basic_filebuf::open(__s,__mode|in).  If that function
00662        *  fails, @c failbit is set in the stream's error state.
00663        */
00664       template<typename _Path>
00665         _If_fs_path<_Path, void>
00666         open(const _Path& __s, ios_base::openmode __mode = ios_base::in)
00667         { open(__s.c_str(), __mode); }
00668 #endif // C++17
00669 #endif // C++11
00670 
00671       /**
00672        *  @brief  Close the file.
00673        *
00674        *  Calls @c std::basic_filebuf::close().  If that function
00675        *  fails, @c failbit is set in the stream's error state.
00676        */
00677       void
00678       close()
00679       {
00680         if (!_M_filebuf.close())
00681           this->setstate(ios_base::failbit);
00682       }
00683     };
00684 
00685 
00686   // [27.8.1.8] Template class basic_ofstream
00687   /**
00688    *  @brief  Controlling output for files.
00689    *  @ingroup io
00690    *
00691    *  @tparam _CharT  Type of character stream.
00692    *  @tparam _Traits  Traits for character type, defaults to
00693    *                   char_traits<_CharT>.
00694    *
00695    *  This class supports reading from named files, using the inherited
00696    *  functions from std::basic_ostream.  To control the associated
00697    *  sequence, an instance of std::basic_filebuf is used, which this page
00698    *  refers to as @c sb.
00699    */
00700   template<typename _CharT, typename _Traits>
00701     class basic_ofstream : public basic_ostream<_CharT,_Traits>
00702     {
00703     public:
00704       // Types:
00705       typedef _CharT                                    char_type;
00706       typedef _Traits                                   traits_type;
00707       typedef typename traits_type::int_type            int_type;
00708       typedef typename traits_type::pos_type            pos_type;
00709       typedef typename traits_type::off_type            off_type;
00710 
00711       // Non-standard types:
00712       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00713       typedef basic_ostream<char_type, traits_type>     __ostream_type;
00714 
00715     private:
00716       __filebuf_type    _M_filebuf;
00717 
00718     public:
00719       // Constructors:
00720       /**
00721        *  @brief  Default constructor.
00722        *
00723        *  Initializes @c sb using its default constructor, and passes
00724        *  @c &sb to the base class initializer.  Does not open any files
00725        *  (you haven't given it a filename to open).
00726        */
00727       basic_ofstream(): __ostream_type(), _M_filebuf()
00728       { this->init(&_M_filebuf); }
00729 
00730       /**
00731        *  @brief  Create an output file stream.
00732        *  @param  __s  Null terminated string specifying the filename.
00733        *  @param  __mode  Open file in specified mode (see std::ios_base).
00734        *
00735        *  @c ios_base::out is automatically included in @a __mode.
00736        */
00737       explicit
00738       basic_ofstream(const char* __s,
00739                      ios_base::openmode __mode = ios_base::out)
00740       : __ostream_type(), _M_filebuf()
00741       {
00742         this->init(&_M_filebuf);
00743         this->open(__s, __mode);
00744       }
00745 
00746 #if __cplusplus >= 201103L
00747       /**
00748        *  @brief  Create an output file stream.
00749        *  @param  __s  std::string specifying the filename.
00750        *  @param  __mode  Open file in specified mode (see std::ios_base).
00751        *
00752        *  @c ios_base::out is automatically included in @a __mode.
00753        */
00754       explicit
00755       basic_ofstream(const std::string& __s,
00756                      ios_base::openmode __mode = ios_base::out)
00757       : __ostream_type(), _M_filebuf()
00758       {
00759         this->init(&_M_filebuf);
00760         this->open(__s, __mode);
00761       }
00762 
00763 #if __cplusplus >= 201703L
00764       /**
00765        *  @param  Create an output file stream.
00766        *  @param  __s  filesystem::path specifying the filename.
00767        *  @param  __mode  Open file in specified mode (see std::ios_base).
00768        *
00769        *  @c ios_base::out is automatically included in @a __mode.
00770        */
00771       template<typename _Path, typename _Require = _If_fs_path<_Path>>
00772         basic_ofstream(const _Path& __s,
00773                        ios_base::openmode __mode = ios_base::out)
00774         : basic_ofstream(__s.c_str(), __mode)
00775         { }
00776 #endif // C++17
00777 
00778       basic_ofstream(const basic_ofstream&) = delete;
00779 
00780       basic_ofstream(basic_ofstream&& __rhs)
00781       : __ostream_type(std::move(__rhs)),
00782       _M_filebuf(std::move(__rhs._M_filebuf))
00783       { __ostream_type::set_rdbuf(&_M_filebuf); }
00784 #endif
00785 
00786       /**
00787        *  @brief  The destructor does nothing.
00788        *
00789        *  The file is closed by the filebuf object, not the formatting
00790        *  stream.
00791        */
00792       ~basic_ofstream()
00793       { }
00794 
00795 #if __cplusplus >= 201103L
00796       // 27.8.3.2 Assign and swap:
00797 
00798       basic_ofstream&
00799       operator=(const basic_ofstream&) = delete;
00800 
00801       basic_ofstream&
00802       operator=(basic_ofstream&& __rhs)
00803       {
00804         __ostream_type::operator=(std::move(__rhs));
00805         _M_filebuf = std::move(__rhs._M_filebuf);
00806         return *this;
00807       }
00808 
00809       void
00810       swap(basic_ofstream& __rhs)
00811       {
00812         __ostream_type::swap(__rhs);
00813         _M_filebuf.swap(__rhs._M_filebuf);
00814       }
00815 #endif
00816 
00817       // Members:
00818       /**
00819        *  @brief  Accessing the underlying buffer.
00820        *  @return  The current basic_filebuf buffer.
00821        *
00822        *  This hides both signatures of std::basic_ios::rdbuf().
00823        */
00824       __filebuf_type*
00825       rdbuf() const
00826       { return const_cast<__filebuf_type*>(&_M_filebuf); }
00827 
00828       /**
00829        *  @brief  Wrapper to test for an open file.
00830        *  @return  @c rdbuf()->is_open()
00831        */
00832       bool
00833       is_open()
00834       { return _M_filebuf.is_open(); }
00835 
00836       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00837       // 365. Lack of const-qualification in clause 27
00838       bool
00839       is_open() const
00840       { return _M_filebuf.is_open(); }
00841 
00842       /**
00843        *  @brief  Opens an external file.
00844        *  @param  __s  The name of the file.
00845        *  @param  __mode  The open mode flags.
00846        *
00847        *  Calls @c std::basic_filebuf::open(__s,__mode|out).  If that
00848        *  function fails, @c failbit is set in the stream's error state.
00849        */
00850       void
00851       open(const char* __s, ios_base::openmode __mode = ios_base::out)
00852       {
00853         if (!_M_filebuf.open(__s, __mode | ios_base::out))
00854           this->setstate(ios_base::failbit);
00855         else
00856           // _GLIBCXX_RESOLVE_LIB_DEFECTS
00857           // 409. Closing an fstream should clear error state
00858           this->clear();
00859       }
00860 
00861 #if __cplusplus >= 201103L
00862       /**
00863        *  @brief  Opens an external file.
00864        *  @param  __s  The name of the file.
00865        *  @param  __mode  The open mode flags.
00866        *
00867        *  Calls @c std::basic_filebuf::open(s,mode|out).  If that
00868        *  function fails, @c failbit is set in the stream's error state.
00869        */
00870       void
00871       open(const std::string& __s, ios_base::openmode __mode = ios_base::out)
00872       {
00873         if (!_M_filebuf.open(__s, __mode | ios_base::out))
00874           this->setstate(ios_base::failbit);
00875         else
00876           // _GLIBCXX_RESOLVE_LIB_DEFECTS
00877           // 409. Closing an fstream should clear error state
00878           this->clear();
00879       }
00880 
00881 #if __cplusplus >= 201703L
00882       /**
00883        *  @brief  Opens an external file.
00884        *  @param  __s  The name of the file, as a filesystem::path.
00885        *  @param  __mode  The open mode flags.
00886        *
00887        *  Calls @c std::basic_filebuf::open(__s,__mode|out).  If that
00888        *  function fails, @c failbit is set in the stream's error state.
00889        */
00890       template<typename _Path>
00891         _If_fs_path<_Path, void>
00892         open(const _Path& __s, ios_base::openmode __mode = ios_base::out)
00893         { open(__s.c_str(), __mode); }
00894 #endif // C++17
00895 #endif // C++11
00896 
00897       /**
00898        *  @brief  Close the file.
00899        *
00900        *  Calls @c std::basic_filebuf::close().  If that function
00901        *  fails, @c failbit is set in the stream's error state.
00902        */
00903       void
00904       close()
00905       {
00906         if (!_M_filebuf.close())
00907           this->setstate(ios_base::failbit);
00908       }
00909     };
00910 
00911 
00912   // [27.8.1.11] Template class basic_fstream
00913   /**
00914    *  @brief  Controlling input and output for files.
00915    *  @ingroup io
00916    *
00917    *  @tparam _CharT  Type of character stream.
00918    *  @tparam _Traits  Traits for character type, defaults to
00919    *                   char_traits<_CharT>.
00920    *
00921    *  This class supports reading from and writing to named files, using
00922    *  the inherited functions from std::basic_iostream.  To control the
00923    *  associated sequence, an instance of std::basic_filebuf is used, which
00924    *  this page refers to as @c sb.
00925    */
00926   template<typename _CharT, typename _Traits>
00927     class basic_fstream : public basic_iostream<_CharT, _Traits>
00928     {
00929     public:
00930       // Types:
00931       typedef _CharT                                    char_type;
00932       typedef _Traits                                   traits_type;
00933       typedef typename traits_type::int_type            int_type;
00934       typedef typename traits_type::pos_type            pos_type;
00935       typedef typename traits_type::off_type            off_type;
00936 
00937       // Non-standard types:
00938       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00939       typedef basic_ios<char_type, traits_type>         __ios_type;
00940       typedef basic_iostream<char_type, traits_type>    __iostream_type;
00941 
00942     private:
00943       __filebuf_type    _M_filebuf;
00944 
00945     public:
00946       // Constructors/destructor:
00947       /**
00948        *  @brief  Default constructor.
00949        *
00950        *  Initializes @c sb using its default constructor, and passes
00951        *  @c &sb to the base class initializer.  Does not open any files
00952        *  (you haven't given it a filename to open).
00953        */
00954       basic_fstream()
00955       : __iostream_type(), _M_filebuf()
00956       { this->init(&_M_filebuf); }
00957 
00958       /**
00959        *  @brief  Create an input/output file stream.
00960        *  @param  __s  Null terminated string specifying the filename.
00961        *  @param  __mode  Open file in specified mode (see std::ios_base).
00962        */
00963       explicit
00964       basic_fstream(const char* __s,
00965                     ios_base::openmode __mode = ios_base::in | ios_base::out)
00966       : __iostream_type(0), _M_filebuf()
00967       {
00968         this->init(&_M_filebuf);
00969         this->open(__s, __mode);
00970       }
00971 
00972 #if __cplusplus >= 201103L
00973       /**
00974        *  @brief  Create an input/output file stream.
00975        *  @param  __s  Null terminated string specifying the filename.
00976        *  @param  __mode  Open file in specified mode (see std::ios_base).
00977        */
00978       explicit
00979       basic_fstream(const std::string& __s,
00980                     ios_base::openmode __mode = ios_base::in | ios_base::out)
00981       : __iostream_type(0), _M_filebuf()
00982       {
00983         this->init(&_M_filebuf);
00984         this->open(__s, __mode);
00985       }
00986 
00987 #if __cplusplus >= 201703L
00988       /**
00989        *  @param  Create an input/output file stream.
00990        *  @param  __s  filesystem::path specifying the filename.
00991        *  @param  __mode  Open file in specified mode (see std::ios_base).
00992        */
00993       template<typename _Path, typename _Require = _If_fs_path<_Path>>
00994         basic_fstream(const _Path& __s,
00995                       ios_base::openmode __mode = ios_base::in | ios_base::out)
00996         : basic_fstream(__s.c_str(), __mode)
00997         { }
00998 #endif // C++17
00999 
01000       basic_fstream(const basic_fstream&) = delete;
01001 
01002       basic_fstream(basic_fstream&& __rhs)
01003       : __iostream_type(std::move(__rhs)),
01004       _M_filebuf(std::move(__rhs._M_filebuf))
01005       { __iostream_type::set_rdbuf(&_M_filebuf); }
01006 #endif
01007 
01008       /**
01009        *  @brief  The destructor does nothing.
01010        *
01011        *  The file is closed by the filebuf object, not the formatting
01012        *  stream.
01013        */
01014       ~basic_fstream()
01015       { }
01016 
01017 #if __cplusplus >= 201103L
01018       // 27.8.3.2 Assign and swap:
01019 
01020       basic_fstream&
01021       operator=(const basic_fstream&) = delete;
01022 
01023       basic_fstream&
01024       operator=(basic_fstream&& __rhs)
01025       {
01026         __iostream_type::operator=(std::move(__rhs));
01027         _M_filebuf = std::move(__rhs._M_filebuf);
01028         return *this;
01029       }
01030 
01031       void
01032       swap(basic_fstream& __rhs)
01033       {
01034         __iostream_type::swap(__rhs);
01035         _M_filebuf.swap(__rhs._M_filebuf);
01036       }
01037 #endif
01038 
01039       // Members:
01040       /**
01041        *  @brief  Accessing the underlying buffer.
01042        *  @return  The current basic_filebuf buffer.
01043        *
01044        *  This hides both signatures of std::basic_ios::rdbuf().
01045        */
01046       __filebuf_type*
01047       rdbuf() const
01048       { return const_cast<__filebuf_type*>(&_M_filebuf); }
01049 
01050       /**
01051        *  @brief  Wrapper to test for an open file.
01052        *  @return  @c rdbuf()->is_open()
01053        */
01054       bool
01055       is_open()
01056       { return _M_filebuf.is_open(); }
01057 
01058       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01059       // 365. Lack of const-qualification in clause 27
01060       bool
01061       is_open() const
01062       { return _M_filebuf.is_open(); }
01063 
01064       /**
01065        *  @brief  Opens an external file.
01066        *  @param  __s  The name of the file.
01067        *  @param  __mode  The open mode flags.
01068        *
01069        *  Calls @c std::basic_filebuf::open(__s,__mode).  If that
01070        *  function fails, @c failbit is set in the stream's error state.
01071        */
01072       void
01073       open(const char* __s,
01074            ios_base::openmode __mode = ios_base::in | ios_base::out)
01075       {
01076         if (!_M_filebuf.open(__s, __mode))
01077           this->setstate(ios_base::failbit);
01078         else
01079           // _GLIBCXX_RESOLVE_LIB_DEFECTS
01080           // 409. Closing an fstream should clear error state
01081           this->clear();
01082       }
01083 
01084 #if __cplusplus >= 201103L
01085       /**
01086        *  @brief  Opens an external file.
01087        *  @param  __s  The name of the file.
01088        *  @param  __mode  The open mode flags.
01089        *
01090        *  Calls @c std::basic_filebuf::open(__s,__mode).  If that
01091        *  function fails, @c failbit is set in the stream's error state.
01092        */
01093       void
01094       open(const std::string& __s,
01095            ios_base::openmode __mode = ios_base::in | ios_base::out)
01096       {
01097         if (!_M_filebuf.open(__s, __mode))
01098           this->setstate(ios_base::failbit);
01099         else
01100           // _GLIBCXX_RESOLVE_LIB_DEFECTS
01101           // 409. Closing an fstream should clear error state
01102           this->clear();
01103       }
01104 
01105 #if __cplusplus >= 201703L
01106       /**
01107        *  @brief  Opens an external file.
01108        *  @param  __s  The name of the file, as a filesystem::path.
01109        *  @param  __mode  The open mode flags.
01110        *
01111        *  Calls @c std::basic_filebuf::open(__s,__mode).  If that
01112        *  function fails, @c failbit is set in the stream's error state.
01113        */
01114       template<typename _Path>
01115         _If_fs_path<_Path, void>
01116         open(const _Path& __s,
01117              ios_base::openmode __mode = ios_base::in | ios_base::out)
01118         { open(__s.c_str(), __mode); }
01119 #endif // C++17
01120 #endif // C++11
01121 
01122       /**
01123        *  @brief  Close the file.
01124        *
01125        *  Calls @c std::basic_filebuf::close().  If that function
01126        *  fails, @c failbit is set in the stream's error state.
01127        */
01128       void
01129       close()
01130       {
01131         if (!_M_filebuf.close())
01132           this->setstate(ios_base::failbit);
01133       }
01134     };
01135 
01136 #if __cplusplus >= 201103L
01137   /// Swap specialization for filebufs.
01138   template <class _CharT, class _Traits>
01139     inline void
01140     swap(basic_filebuf<_CharT, _Traits>& __x,
01141          basic_filebuf<_CharT, _Traits>& __y)
01142     { __x.swap(__y); }
01143 
01144   /// Swap specialization for ifstreams.
01145   template <class _CharT, class _Traits>
01146     inline void
01147     swap(basic_ifstream<_CharT, _Traits>& __x,
01148          basic_ifstream<_CharT, _Traits>& __y)
01149     { __x.swap(__y); }
01150 
01151   /// Swap specialization for ofstreams.
01152   template <class _CharT, class _Traits>
01153     inline void
01154     swap(basic_ofstream<_CharT, _Traits>& __x,
01155          basic_ofstream<_CharT, _Traits>& __y)
01156     { __x.swap(__y); }
01157 
01158   /// Swap specialization for fstreams.
01159   template <class _CharT, class _Traits>
01160     inline void
01161     swap(basic_fstream<_CharT, _Traits>& __x,
01162          basic_fstream<_CharT, _Traits>& __y)
01163     { __x.swap(__y); }
01164 #endif
01165 
01166 _GLIBCXX_END_NAMESPACE_VERSION
01167 } // namespace
01168 
01169 #include <bits/fstream.tcc>
01170 
01171 #endif /* _GLIBCXX_FSTREAM */