1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997-2018 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/fstream
26 * This is a Standard C++ Library header.
30 // ISO C++ 14882: 27.8 File-based streams
33 #ifndef _GLIBCXX_FSTREAM
34 #define _GLIBCXX_FSTREAM 1
36 #pragma GCC system_header
40 #include <bits/codecvt.h>
41 #include <cstdio> // For BUFSIZ
42 #include <bits/basic_file.h> // For __basic_file, __c_lock
43 #if __cplusplus >= 201103L
44 #include <string> // For std::string overloads.
47 namespace std _GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 #if __cplusplus >= 201703L
52 // Enable if _Path is a filesystem::path or experimental::filesystem::path
53 template<typename _Path, typename _Result = _Path, typename _Path2
54 = decltype(std::declval<_Path&>().make_preferred().filename())>
55 using _If_fs_path = enable_if_t<is_same_v<_Path, _Path2>, _Result>;
59 // [27.8.1.1] template class basic_filebuf
61 * @brief The actual work of input and output (for files).
64 * @tparam _CharT Type of character stream.
65 * @tparam _Traits Traits for character type, defaults to
66 * char_traits<_CharT>.
68 * This class associates both its input and output sequence with an
69 * external disk file, and maintains a joint file position for both
70 * sequences. Many of its semantics are described in terms of similar
71 * behavior in the Standard C Library's @c FILE streams.
73 * Requirements on traits_type, specific to this class:
74 * - traits_type::pos_type must be fpos<traits_type::state_type>
75 * - traits_type::off_type must be streamoff
76 * - traits_type::state_type must be Assignable and DefaultConstructible,
77 * - traits_type::state_type() must be the initial state for codecvt.
79 template<typename _CharT, typename _Traits>
80 class basic_filebuf : public basic_streambuf<_CharT, _Traits>
82 #if __cplusplus >= 201103L
83 template<typename _Tp>
84 using __chk_state = __and_<is_copy_assignable<_Tp>,
85 is_copy_constructible<_Tp>,
86 is_default_constructible<_Tp>>;
88 static_assert(__chk_state<typename _Traits::state_type>::value,
89 "state_type must be CopyAssignable, CopyConstructible"
90 " and DefaultConstructible");
92 static_assert(is_same<typename _Traits::pos_type,
93 fpos<typename _Traits::state_type>>::value,
94 "pos_type must be fpos<state_type>");
98 typedef _CharT char_type;
99 typedef _Traits traits_type;
100 typedef typename traits_type::int_type int_type;
101 typedef typename traits_type::pos_type pos_type;
102 typedef typename traits_type::off_type off_type;
104 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
105 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
106 typedef __basic_file<char> __file_type;
107 typedef typename traits_type::state_type __state_type;
108 typedef codecvt<char_type, char, __state_type> __codecvt_type;
110 friend class ios_base; // For sync_with_stdio.
114 // MT lock inherited from libio or other low-level io library.
120 /// Place to stash in || out || in | out settings for current filebuf.
121 ios_base::openmode _M_mode;
123 // Beginning state type for codecvt.
124 __state_type _M_state_beg;
126 // During output, the state that corresponds to pptr(),
127 // during input, the state that corresponds to egptr() and
129 __state_type _M_state_cur;
131 // Not used for output. During input, the state that corresponds
132 // to eback() and _M_ext_buf.
133 __state_type _M_state_last;
135 /// Pointer to the beginning of internal buffer.
139 * Actual size of internal buffer. This number is equal to the size
140 * of the put area + 1 position, reserved for the overflow char of
145 // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
146 bool _M_buf_allocated;
149 * _M_reading == false && _M_writing == false for @b uncommitted mode;
150 * _M_reading == true for @b read mode;
151 * _M_writing == true for @b write mode;
153 * NB: _M_reading == true && _M_writing == true is unused.
160 * Necessary bits for putback buffer management.
162 * @note pbacks of over one character are not currently supported.
165 char_type* _M_pback_cur_save;
166 char_type* _M_pback_end_save;
170 // Cached codecvt facet.
171 const __codecvt_type* _M_codecvt;
174 * Buffer for external characters. Used for input when
175 * codecvt::always_noconv() == false. When valid, this corresponds
181 * Size of buffer held by _M_ext_buf.
183 streamsize _M_ext_buf_size;
186 * Pointers into the buffer held by _M_ext_buf that delimit a
187 * subsequence of bytes that have been read but not yet converted.
188 * When valid, _M_ext_next corresponds to egptr().
190 const char* _M_ext_next;
194 * Initializes pback buffers, and moves normal buffers to safety.
196 * _M_in_cur has already been moved back
203 _M_pback_cur_save = this->gptr();
204 _M_pback_end_save = this->egptr();
205 this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
206 _M_pback_init = true;
211 * Deactivates pback buffer contents, and restores normal buffer.
213 * The pback buffer has only moved forward.
216 _M_destroy_pback() throw()
220 // Length _M_in_cur moved in the pback buffer.
221 _M_pback_cur_save += this->gptr() != this->eback();
222 this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
223 _M_pback_init = false;
228 // Constructors/destructor:
230 * @brief Does not open any files.
232 * The default constructor initializes the parent class using its
237 #if __cplusplus >= 201103L
238 basic_filebuf(const basic_filebuf&) = delete;
239 basic_filebuf(basic_filebuf&&);
243 * @brief The destructor closes the file first.
249 #if __cplusplus >= 201103L
250 basic_filebuf& operator=(const basic_filebuf&) = delete;
251 basic_filebuf& operator=(basic_filebuf&&);
252 void swap(basic_filebuf&);
257 * @brief Returns true if the external file is open.
260 is_open() const throw()
261 { return _M_file.is_open(); }
264 * @brief Opens an external file.
265 * @param __s The name of the file.
266 * @param __mode The open mode flags.
267 * @return @c this on success, NULL on failure
269 * If a file is already open, this function immediately fails.
270 * Otherwise it tries to open the file named @a __s using the flags
271 * given in @a __mode.
273 * Table 92, adapted here, gives the relation between openmode
274 * combinations and the equivalent @c fopen() flags.
275 * (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,
276 * and binary|in|app per DR 596)
278 * +---------------------------------------------------------+
279 * | ios_base Flag combination stdio equivalent |
280 * |binary in out trunc app |
281 * +---------------------------------------------------------+
291 * +---------------------------------------------------------+
301 * +---------------------------------------------------------+
305 open(const char* __s, ios_base::openmode __mode);
307 #if __cplusplus >= 201103L
309 * @brief Opens an external file.
310 * @param __s The name of the file.
311 * @param __mode The open mode flags.
312 * @return @c this on success, NULL on failure
315 open(const std::string& __s, ios_base::openmode __mode)
316 { return open(__s.c_str(), __mode); }
318 #if __cplusplus >= 201703L
320 * @brief Opens an external file.
321 * @param __s The name of the file, as a filesystem::path.
322 * @param __mode The open mode flags.
323 * @return @c this on success, NULL on failure
325 template<typename _Path>
326 _If_fs_path<_Path, __filebuf_type*>
327 open(const _Path& __s, ios_base::openmode __mode)
328 { return open(__s.c_str(), __mode); }
333 * @brief Closes the currently associated file.
334 * @return @c this on success, NULL on failure
336 * If no file is currently open, this function immediately fails.
338 * If a <em>put buffer area</em> exists, @c overflow(eof) is
339 * called to flush all the characters. The file is then
342 * If any operations fail, this function also fails.
349 _M_allocate_internal_buffer();
352 _M_destroy_internal_buffer() throw();
354 // [27.8.1.4] overridden virtual functions
358 // Stroustrup, 1998, p. 628
359 // underflow() and uflow() functions are called to get the next
360 // character from the real input source when the buffer is empty.
361 // Buffered input uses underflow()
367 pbackfail(int_type __c = _Traits::eof());
369 // Stroustrup, 1998, p 648
370 // The overflow() function is called to transfer characters to the
371 // real output destination when the buffer is full. A call to
372 // overflow(c) outputs the contents of the buffer plus the
375 // Consume some sequence of the characters in the pending sequence.
377 overflow(int_type __c = _Traits::eof());
379 // Convert internal byte sequence to external, char-based
380 // sequence via codecvt.
382 _M_convert_to_external(char_type*, streamsize);
385 * @brief Manipulates the buffer.
386 * @param __s Pointer to a buffer area.
387 * @param __n Size of @a __s.
390 * If no file has been opened, and both @a __s and @a __n are zero, then
391 * the stream becomes unbuffered. Otherwise, @c __s is used as a
393 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
396 virtual __streambuf_type*
397 setbuf(char_type* __s, streamsize __n);
400 seekoff(off_type __off, ios_base::seekdir __way,
401 ios_base::openmode __mode = ios_base::in | ios_base::out);
404 seekpos(pos_type __pos,
405 ios_base::openmode __mode = ios_base::in | ios_base::out);
407 // Common code for seekoff, seekpos, and overflow
409 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
412 _M_get_ext_pos(__state_type &__state);
418 imbue(const locale& __loc);
421 xsgetn(char_type* __s, streamsize __n);
424 xsputn(const char_type* __s, streamsize __n);
426 // Flushes output buffer, then writes unshift sequence.
428 _M_terminate_output();
431 * This function sets the pointers of the internal buffer, both get
432 * and put areas. Typically:
434 * __off == egptr() - eback() upon underflow/uflow (@b read mode);
435 * __off == 0 upon overflow (@b write mode);
436 * __off == -1 upon open, setbuf, seekoff/pos (@b uncommitted mode).
438 * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
439 * reflects the actual allocated memory and the last cell is reserved
440 * for the overflow char of a full put area.
443 _M_set_buffer(streamsize __off)
445 const bool __testin = _M_mode & ios_base::in;
446 const bool __testout = (_M_mode & ios_base::out
447 || _M_mode & ios_base::app);
449 if (__testin && __off > 0)
450 this->setg(_M_buf, _M_buf, _M_buf + __off);
452 this->setg(_M_buf, _M_buf, _M_buf);
454 if (__testout && __off == 0 && _M_buf_size > 1 )
455 this->setp(_M_buf, _M_buf + _M_buf_size - 1);
461 // [27.8.1.5] Template class basic_ifstream
463 * @brief Controlling input for files.
466 * @tparam _CharT Type of character stream.
467 * @tparam _Traits Traits for character type, defaults to
468 * char_traits<_CharT>.
470 * This class supports reading from named files, using the inherited
471 * functions from std::basic_istream. To control the associated
472 * sequence, an instance of std::basic_filebuf is used, which this page
473 * refers to as @c sb.
475 template<typename _CharT, typename _Traits>
476 class basic_ifstream : public basic_istream<_CharT, _Traits>
480 typedef _CharT char_type;
481 typedef _Traits traits_type;
482 typedef typename traits_type::int_type int_type;
483 typedef typename traits_type::pos_type pos_type;
484 typedef typename traits_type::off_type off_type;
486 // Non-standard types:
487 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
488 typedef basic_istream<char_type, traits_type> __istream_type;
491 __filebuf_type _M_filebuf;
494 // Constructors/Destructors:
496 * @brief Default constructor.
498 * Initializes @c sb using its default constructor, and passes
499 * @c &sb to the base class initializer. Does not open any files
500 * (you haven't given it a filename to open).
502 basic_ifstream() : __istream_type(), _M_filebuf()
503 { this->init(&_M_filebuf); }
506 * @brief Create an input file stream.
507 * @param __s Null terminated string specifying the filename.
508 * @param __mode Open file in specified mode (see std::ios_base).
510 * @c ios_base::in is automatically included in @a __mode.
513 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
514 : __istream_type(), _M_filebuf()
516 this->init(&_M_filebuf);
517 this->open(__s, __mode);
520 #if __cplusplus >= 201103L
522 * @brief Create an input file stream.
523 * @param __s std::string specifying the filename.
524 * @param __mode Open file in specified mode (see std::ios_base).
526 * @c ios_base::in is automatically included in @a __mode.
529 basic_ifstream(const std::string& __s,
530 ios_base::openmode __mode = ios_base::in)
531 : __istream_type(), _M_filebuf()
533 this->init(&_M_filebuf);
534 this->open(__s, __mode);
537 #if __cplusplus >= 201703L
539 * @param Create an input file stream.
540 * @param __s filesystem::path specifying the filename.
541 * @param __mode Open file in specified mode (see std::ios_base).
543 * @c ios_base::in is automatically included in @a __mode.
545 template<typename _Path, typename _Require = _If_fs_path<_Path>>
546 basic_ifstream(const _Path& __s,
547 ios_base::openmode __mode = ios_base::in)
548 : basic_ifstream(__s.c_str(), __mode)
552 basic_ifstream(const basic_ifstream&) = delete;
554 basic_ifstream(basic_ifstream&& __rhs)
555 : __istream_type(std::move(__rhs)),
556 _M_filebuf(std::move(__rhs._M_filebuf))
557 { __istream_type::set_rdbuf(&_M_filebuf); }
561 * @brief The destructor does nothing.
563 * The file is closed by the filebuf object, not the formatting
569 #if __cplusplus >= 201103L
570 // 27.8.3.2 Assign and swap:
573 operator=(const basic_ifstream&) = delete;
576 operator=(basic_ifstream&& __rhs)
578 __istream_type::operator=(std::move(__rhs));
579 _M_filebuf = std::move(__rhs._M_filebuf);
584 swap(basic_ifstream& __rhs)
586 __istream_type::swap(__rhs);
587 _M_filebuf.swap(__rhs._M_filebuf);
593 * @brief Accessing the underlying buffer.
594 * @return The current basic_filebuf buffer.
596 * This hides both signatures of std::basic_ios::rdbuf().
600 { return const_cast<__filebuf_type*>(&_M_filebuf); }
603 * @brief Wrapper to test for an open file.
604 * @return @c rdbuf()->is_open()
608 { return _M_filebuf.is_open(); }
610 // _GLIBCXX_RESOLVE_LIB_DEFECTS
611 // 365. Lack of const-qualification in clause 27
614 { return _M_filebuf.is_open(); }
617 * @brief Opens an external file.
618 * @param __s The name of the file.
619 * @param __mode The open mode flags.
621 * Calls @c std::basic_filebuf::open(s,__mode|in). If that function
622 * fails, @c failbit is set in the stream's error state.
625 open(const char* __s, ios_base::openmode __mode = ios_base::in)
627 if (!_M_filebuf.open(__s, __mode | ios_base::in))
628 this->setstate(ios_base::failbit);
630 // _GLIBCXX_RESOLVE_LIB_DEFECTS
631 // 409. Closing an fstream should clear error state
635 #if __cplusplus >= 201103L
637 * @brief Opens an external file.
638 * @param __s The name of the file.
639 * @param __mode The open mode flags.
641 * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
642 * fails, @c failbit is set in the stream's error state.
645 open(const std::string& __s, ios_base::openmode __mode = ios_base::in)
647 if (!_M_filebuf.open(__s, __mode | ios_base::in))
648 this->setstate(ios_base::failbit);
650 // _GLIBCXX_RESOLVE_LIB_DEFECTS
651 // 409. Closing an fstream should clear error state
655 #if __cplusplus >= 201703L
657 * @brief Opens an external file.
658 * @param __s The name of the file, as a filesystem::path.
659 * @param __mode The open mode flags.
661 * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
662 * fails, @c failbit is set in the stream's error state.
664 template<typename _Path>
665 _If_fs_path<_Path, void>
666 open(const _Path& __s, ios_base::openmode __mode = ios_base::in)
667 { open(__s.c_str(), __mode); }
672 * @brief Close the file.
674 * Calls @c std::basic_filebuf::close(). If that function
675 * fails, @c failbit is set in the stream's error state.
680 if (!_M_filebuf.close())
681 this->setstate(ios_base::failbit);
686 // [27.8.1.8] Template class basic_ofstream
688 * @brief Controlling output for files.
691 * @tparam _CharT Type of character stream.
692 * @tparam _Traits Traits for character type, defaults to
693 * char_traits<_CharT>.
695 * This class supports reading from named files, using the inherited
696 * functions from std::basic_ostream. To control the associated
697 * sequence, an instance of std::basic_filebuf is used, which this page
698 * refers to as @c sb.
700 template<typename _CharT, typename _Traits>
701 class basic_ofstream : public basic_ostream<_CharT,_Traits>
705 typedef _CharT char_type;
706 typedef _Traits traits_type;
707 typedef typename traits_type::int_type int_type;
708 typedef typename traits_type::pos_type pos_type;
709 typedef typename traits_type::off_type off_type;
711 // Non-standard types:
712 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
713 typedef basic_ostream<char_type, traits_type> __ostream_type;
716 __filebuf_type _M_filebuf;
721 * @brief Default constructor.
723 * Initializes @c sb using its default constructor, and passes
724 * @c &sb to the base class initializer. Does not open any files
725 * (you haven't given it a filename to open).
727 basic_ofstream(): __ostream_type(), _M_filebuf()
728 { this->init(&_M_filebuf); }
731 * @brief Create an output file stream.
732 * @param __s Null terminated string specifying the filename.
733 * @param __mode Open file in specified mode (see std::ios_base).
735 * @c ios_base::out is automatically included in @a __mode.
738 basic_ofstream(const char* __s,
739 ios_base::openmode __mode = ios_base::out)
740 : __ostream_type(), _M_filebuf()
742 this->init(&_M_filebuf);
743 this->open(__s, __mode);
746 #if __cplusplus >= 201103L
748 * @brief Create an output file stream.
749 * @param __s std::string specifying the filename.
750 * @param __mode Open file in specified mode (see std::ios_base).
752 * @c ios_base::out is automatically included in @a __mode.
755 basic_ofstream(const std::string& __s,
756 ios_base::openmode __mode = ios_base::out)
757 : __ostream_type(), _M_filebuf()
759 this->init(&_M_filebuf);
760 this->open(__s, __mode);
763 #if __cplusplus >= 201703L
765 * @param Create an output file stream.
766 * @param __s filesystem::path specifying the filename.
767 * @param __mode Open file in specified mode (see std::ios_base).
769 * @c ios_base::out is automatically included in @a __mode.
771 template<typename _Path, typename _Require = _If_fs_path<_Path>>
772 basic_ofstream(const _Path& __s,
773 ios_base::openmode __mode = ios_base::out)
774 : basic_ofstream(__s.c_str(), __mode)
778 basic_ofstream(const basic_ofstream&) = delete;
780 basic_ofstream(basic_ofstream&& __rhs)
781 : __ostream_type(std::move(__rhs)),
782 _M_filebuf(std::move(__rhs._M_filebuf))
783 { __ostream_type::set_rdbuf(&_M_filebuf); }
787 * @brief The destructor does nothing.
789 * The file is closed by the filebuf object, not the formatting
795 #if __cplusplus >= 201103L
796 // 27.8.3.2 Assign and swap:
799 operator=(const basic_ofstream&) = delete;
802 operator=(basic_ofstream&& __rhs)
804 __ostream_type::operator=(std::move(__rhs));
805 _M_filebuf = std::move(__rhs._M_filebuf);
810 swap(basic_ofstream& __rhs)
812 __ostream_type::swap(__rhs);
813 _M_filebuf.swap(__rhs._M_filebuf);
819 * @brief Accessing the underlying buffer.
820 * @return The current basic_filebuf buffer.
822 * This hides both signatures of std::basic_ios::rdbuf().
826 { return const_cast<__filebuf_type*>(&_M_filebuf); }
829 * @brief Wrapper to test for an open file.
830 * @return @c rdbuf()->is_open()
834 { return _M_filebuf.is_open(); }
836 // _GLIBCXX_RESOLVE_LIB_DEFECTS
837 // 365. Lack of const-qualification in clause 27
840 { return _M_filebuf.is_open(); }
843 * @brief Opens an external file.
844 * @param __s The name of the file.
845 * @param __mode The open mode flags.
847 * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
848 * function fails, @c failbit is set in the stream's error state.
851 open(const char* __s, ios_base::openmode __mode = ios_base::out)
853 if (!_M_filebuf.open(__s, __mode | ios_base::out))
854 this->setstate(ios_base::failbit);
856 // _GLIBCXX_RESOLVE_LIB_DEFECTS
857 // 409. Closing an fstream should clear error state
861 #if __cplusplus >= 201103L
863 * @brief Opens an external file.
864 * @param __s The name of the file.
865 * @param __mode The open mode flags.
867 * Calls @c std::basic_filebuf::open(s,mode|out). If that
868 * function fails, @c failbit is set in the stream's error state.
871 open(const std::string& __s, ios_base::openmode __mode = ios_base::out)
873 if (!_M_filebuf.open(__s, __mode | ios_base::out))
874 this->setstate(ios_base::failbit);
876 // _GLIBCXX_RESOLVE_LIB_DEFECTS
877 // 409. Closing an fstream should clear error state
881 #if __cplusplus >= 201703L
883 * @brief Opens an external file.
884 * @param __s The name of the file, as a filesystem::path.
885 * @param __mode The open mode flags.
887 * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
888 * function fails, @c failbit is set in the stream's error state.
890 template<typename _Path>
891 _If_fs_path<_Path, void>
892 open(const _Path& __s, ios_base::openmode __mode = ios_base::out)
893 { open(__s.c_str(), __mode); }
898 * @brief Close the file.
900 * Calls @c std::basic_filebuf::close(). If that function
901 * fails, @c failbit is set in the stream's error state.
906 if (!_M_filebuf.close())
907 this->setstate(ios_base::failbit);
912 // [27.8.1.11] Template class basic_fstream
914 * @brief Controlling input and output for files.
917 * @tparam _CharT Type of character stream.
918 * @tparam _Traits Traits for character type, defaults to
919 * char_traits<_CharT>.
921 * This class supports reading from and writing to named files, using
922 * the inherited functions from std::basic_iostream. To control the
923 * associated sequence, an instance of std::basic_filebuf is used, which
924 * this page refers to as @c sb.
926 template<typename _CharT, typename _Traits>
927 class basic_fstream : public basic_iostream<_CharT, _Traits>
931 typedef _CharT char_type;
932 typedef _Traits traits_type;
933 typedef typename traits_type::int_type int_type;
934 typedef typename traits_type::pos_type pos_type;
935 typedef typename traits_type::off_type off_type;
937 // Non-standard types:
938 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
939 typedef basic_ios<char_type, traits_type> __ios_type;
940 typedef basic_iostream<char_type, traits_type> __iostream_type;
943 __filebuf_type _M_filebuf;
946 // Constructors/destructor:
948 * @brief Default constructor.
950 * Initializes @c sb using its default constructor, and passes
951 * @c &sb to the base class initializer. Does not open any files
952 * (you haven't given it a filename to open).
955 : __iostream_type(), _M_filebuf()
956 { this->init(&_M_filebuf); }
959 * @brief Create an input/output file stream.
960 * @param __s Null terminated string specifying the filename.
961 * @param __mode Open file in specified mode (see std::ios_base).
964 basic_fstream(const char* __s,
965 ios_base::openmode __mode = ios_base::in | ios_base::out)
966 : __iostream_type(0), _M_filebuf()
968 this->init(&_M_filebuf);
969 this->open(__s, __mode);
972 #if __cplusplus >= 201103L
974 * @brief Create an input/output file stream.
975 * @param __s Null terminated string specifying the filename.
976 * @param __mode Open file in specified mode (see std::ios_base).
979 basic_fstream(const std::string& __s,
980 ios_base::openmode __mode = ios_base::in | ios_base::out)
981 : __iostream_type(0), _M_filebuf()
983 this->init(&_M_filebuf);
984 this->open(__s, __mode);
987 #if __cplusplus >= 201703L
989 * @param Create an input/output file stream.
990 * @param __s filesystem::path specifying the filename.
991 * @param __mode Open file in specified mode (see std::ios_base).
993 template<typename _Path, typename _Require = _If_fs_path<_Path>>
994 basic_fstream(const _Path& __s,
995 ios_base::openmode __mode = ios_base::in | ios_base::out)
996 : basic_fstream(__s.c_str(), __mode)
1000 basic_fstream(const basic_fstream&) = delete;
1002 basic_fstream(basic_fstream&& __rhs)
1003 : __iostream_type(std::move(__rhs)),
1004 _M_filebuf(std::move(__rhs._M_filebuf))
1005 { __iostream_type::set_rdbuf(&_M_filebuf); }
1009 * @brief The destructor does nothing.
1011 * The file is closed by the filebuf object, not the formatting
1017 #if __cplusplus >= 201103L
1018 // 27.8.3.2 Assign and swap:
1021 operator=(const basic_fstream&) = delete;
1024 operator=(basic_fstream&& __rhs)
1026 __iostream_type::operator=(std::move(__rhs));
1027 _M_filebuf = std::move(__rhs._M_filebuf);
1032 swap(basic_fstream& __rhs)
1034 __iostream_type::swap(__rhs);
1035 _M_filebuf.swap(__rhs._M_filebuf);
1041 * @brief Accessing the underlying buffer.
1042 * @return The current basic_filebuf buffer.
1044 * This hides both signatures of std::basic_ios::rdbuf().
1048 { return const_cast<__filebuf_type*>(&_M_filebuf); }
1051 * @brief Wrapper to test for an open file.
1052 * @return @c rdbuf()->is_open()
1056 { return _M_filebuf.is_open(); }
1058 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1059 // 365. Lack of const-qualification in clause 27
1062 { return _M_filebuf.is_open(); }
1065 * @brief Opens an external file.
1066 * @param __s The name of the file.
1067 * @param __mode The open mode flags.
1069 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1070 * function fails, @c failbit is set in the stream's error state.
1073 open(const char* __s,
1074 ios_base::openmode __mode = ios_base::in | ios_base::out)
1076 if (!_M_filebuf.open(__s, __mode))
1077 this->setstate(ios_base::failbit);
1079 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1080 // 409. Closing an fstream should clear error state
1084 #if __cplusplus >= 201103L
1086 * @brief Opens an external file.
1087 * @param __s The name of the file.
1088 * @param __mode The open mode flags.
1090 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1091 * function fails, @c failbit is set in the stream's error state.
1094 open(const std::string& __s,
1095 ios_base::openmode __mode = ios_base::in | ios_base::out)
1097 if (!_M_filebuf.open(__s, __mode))
1098 this->setstate(ios_base::failbit);
1100 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1101 // 409. Closing an fstream should clear error state
1105 #if __cplusplus >= 201703L
1107 * @brief Opens an external file.
1108 * @param __s The name of the file, as a filesystem::path.
1109 * @param __mode The open mode flags.
1111 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1112 * function fails, @c failbit is set in the stream's error state.
1114 template<typename _Path>
1115 _If_fs_path<_Path, void>
1116 open(const _Path& __s,
1117 ios_base::openmode __mode = ios_base::in | ios_base::out)
1118 { open(__s.c_str(), __mode); }
1123 * @brief Close the file.
1125 * Calls @c std::basic_filebuf::close(). If that function
1126 * fails, @c failbit is set in the stream's error state.
1131 if (!_M_filebuf.close())
1132 this->setstate(ios_base::failbit);
1136 #if __cplusplus >= 201103L
1137 /// Swap specialization for filebufs.
1138 template <class _CharT, class _Traits>
1140 swap(basic_filebuf<_CharT, _Traits>& __x,
1141 basic_filebuf<_CharT, _Traits>& __y)
1144 /// Swap specialization for ifstreams.
1145 template <class _CharT, class _Traits>
1147 swap(basic_ifstream<_CharT, _Traits>& __x,
1148 basic_ifstream<_CharT, _Traits>& __y)
1151 /// Swap specialization for ofstreams.
1152 template <class _CharT, class _Traits>
1154 swap(basic_ofstream<_CharT, _Traits>& __x,
1155 basic_ofstream<_CharT, _Traits>& __y)
1158 /// Swap specialization for fstreams.
1159 template <class _CharT, class _Traits>
1161 swap(basic_fstream<_CharT, _Traits>& __x,
1162 basic_fstream<_CharT, _Traits>& __y)
1166 _GLIBCXX_END_NAMESPACE_VERSION
1169 #include <bits/fstream.tcc>
1171 #endif /* _GLIBCXX_FSTREAM */