1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997-2019 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.
254 #if __cplusplus >= 201103L
255 basic_filebuf& operator=(const basic_filebuf&) = delete;
256 basic_filebuf& operator=(basic_filebuf&&);
257 void swap(basic_filebuf&);
262 * @brief Returns true if the external file is open.
265 is_open() const throw()
266 { return _M_file.is_open(); }
269 * @brief Opens an external file.
270 * @param __s The name of the file.
271 * @param __mode The open mode flags.
272 * @return @c this on success, NULL on failure
274 * If a file is already open, this function immediately fails.
275 * Otherwise it tries to open the file named @a __s using the flags
276 * given in @a __mode.
278 * Table 92, adapted here, gives the relation between openmode
279 * combinations and the equivalent @c fopen() flags.
280 * (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,
281 * and binary|in|app per DR 596)
283 * +---------------------------------------------------------+
284 * | ios_base Flag combination stdio equivalent |
285 * |binary in out trunc app |
286 * +---------------------------------------------------------+
296 * +---------------------------------------------------------+
306 * +---------------------------------------------------------+
310 open(const char* __s, ios_base::openmode __mode);
312 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
314 * @brief Opens an external file.
315 * @param __s The name of the file, as a wide character string.
316 * @param __mode The open mode flags.
317 * @return @c this on success, NULL on failure
320 open(const wchar_t* __s, ios_base::openmode __mode);
323 #if __cplusplus >= 201103L
325 * @brief Opens an external file.
326 * @param __s The name of the file.
327 * @param __mode The open mode flags.
328 * @return @c this on success, NULL on failure
331 open(const std::string& __s, ios_base::openmode __mode)
332 { return open(__s.c_str(), __mode); }
334 #if __cplusplus >= 201703L
336 * @brief Opens an external file.
337 * @param __s The name of the file, as a filesystem::path.
338 * @param __mode The open mode flags.
339 * @return @c this on success, NULL on failure
341 template<typename _Path>
342 _If_fs_path<_Path, __filebuf_type*>
343 open(const _Path& __s, ios_base::openmode __mode)
344 { return open(__s.c_str(), __mode); }
349 * @brief Closes the currently associated file.
350 * @return @c this on success, NULL on failure
352 * If no file is currently open, this function immediately fails.
354 * If a <em>put buffer area</em> exists, @c overflow(eof) is
355 * called to flush all the characters. The file is then
358 * If any operations fail, this function also fails.
365 _M_allocate_internal_buffer();
368 _M_destroy_internal_buffer() throw();
370 // [27.8.1.4] overridden virtual functions
374 // Stroustrup, 1998, p. 628
375 // underflow() and uflow() functions are called to get the next
376 // character from the real input source when the buffer is empty.
377 // Buffered input uses underflow()
383 pbackfail(int_type __c = _Traits::eof());
385 // Stroustrup, 1998, p 648
386 // The overflow() function is called to transfer characters to the
387 // real output destination when the buffer is full. A call to
388 // overflow(c) outputs the contents of the buffer plus the
391 // Consume some sequence of the characters in the pending sequence.
393 overflow(int_type __c = _Traits::eof());
395 // Convert internal byte sequence to external, char-based
396 // sequence via codecvt.
398 _M_convert_to_external(char_type*, streamsize);
401 * @brief Manipulates the buffer.
402 * @param __s Pointer to a buffer area.
403 * @param __n Size of @a __s.
406 * If no file has been opened, and both @a __s and @a __n are zero, then
407 * the stream becomes unbuffered. Otherwise, @c __s is used as a
409 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
412 virtual __streambuf_type*
413 setbuf(char_type* __s, streamsize __n);
416 seekoff(off_type __off, ios_base::seekdir __way,
417 ios_base::openmode __mode = ios_base::in | ios_base::out);
420 seekpos(pos_type __pos,
421 ios_base::openmode __mode = ios_base::in | ios_base::out);
423 // Common code for seekoff, seekpos, and overflow
425 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
428 _M_get_ext_pos(__state_type &__state);
434 imbue(const locale& __loc);
437 xsgetn(char_type* __s, streamsize __n);
440 xsputn(const char_type* __s, streamsize __n);
442 // Flushes output buffer, then writes unshift sequence.
444 _M_terminate_output();
447 * This function sets the pointers of the internal buffer, both get
448 * and put areas. Typically:
450 * __off == egptr() - eback() upon underflow/uflow (@b read mode);
451 * __off == 0 upon overflow (@b write mode);
452 * __off == -1 upon open, setbuf, seekoff/pos (@b uncommitted mode).
454 * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
455 * reflects the actual allocated memory and the last cell is reserved
456 * for the overflow char of a full put area.
459 _M_set_buffer(streamsize __off)
461 const bool __testin = _M_mode & ios_base::in;
462 const bool __testout = (_M_mode & ios_base::out
463 || _M_mode & ios_base::app);
465 if (__testin && __off > 0)
466 this->setg(_M_buf, _M_buf, _M_buf + __off);
468 this->setg(_M_buf, _M_buf, _M_buf);
470 if (__testout && __off == 0 && _M_buf_size > 1 )
471 this->setp(_M_buf, _M_buf + _M_buf_size - 1);
477 // [27.8.1.5] Template class basic_ifstream
479 * @brief Controlling input for files.
482 * @tparam _CharT Type of character stream.
483 * @tparam _Traits Traits for character type, defaults to
484 * char_traits<_CharT>.
486 * This class supports reading from named files, using the inherited
487 * functions from std::basic_istream. To control the associated
488 * sequence, an instance of std::basic_filebuf is used, which this page
489 * refers to as @c sb.
491 template<typename _CharT, typename _Traits>
492 class basic_ifstream : public basic_istream<_CharT, _Traits>
496 typedef _CharT char_type;
497 typedef _Traits traits_type;
498 typedef typename traits_type::int_type int_type;
499 typedef typename traits_type::pos_type pos_type;
500 typedef typename traits_type::off_type off_type;
502 // Non-standard types:
503 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
504 typedef basic_istream<char_type, traits_type> __istream_type;
507 __filebuf_type _M_filebuf;
510 // Constructors/Destructors:
512 * @brief Default constructor.
514 * Initializes @c sb using its default constructor, and passes
515 * @c &sb to the base class initializer. Does not open any files
516 * (you haven't given it a filename to open).
518 basic_ifstream() : __istream_type(), _M_filebuf()
519 { this->init(&_M_filebuf); }
522 * @brief Create an input file stream.
523 * @param __s Null terminated 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 char* __s, ios_base::openmode __mode = ios_base::in)
530 : __istream_type(), _M_filebuf()
532 this->init(&_M_filebuf);
533 this->open(__s, __mode);
536 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
538 * @param Create an input file stream.
539 * @param __s Wide string specifying the filename.
540 * @param __mode Open file in specified mode (see std::ios_base).
542 * @c ios_base::in is automatically included in @a __mode.
544 basic_ifstream(const wchar_t* __s,
545 ios_base::openmode __mode = ios_base::in)
546 : __istream_type(), _M_filebuf()
548 this->init(&_M_filebuf);
549 this->open(__s, __mode);
553 #if __cplusplus >= 201103L
555 * @brief Create an input file stream.
556 * @param __s std::string specifying the filename.
557 * @param __mode Open file in specified mode (see std::ios_base).
559 * @c ios_base::in is automatically included in @a __mode.
562 basic_ifstream(const std::string& __s,
563 ios_base::openmode __mode = ios_base::in)
564 : __istream_type(), _M_filebuf()
566 this->init(&_M_filebuf);
567 this->open(__s, __mode);
570 #if __cplusplus >= 201703L
572 * @param Create an input file stream.
573 * @param __s filesystem::path specifying the filename.
574 * @param __mode Open file in specified mode (see std::ios_base).
576 * @c ios_base::in is automatically included in @a __mode.
578 template<typename _Path, typename _Require = _If_fs_path<_Path>>
579 basic_ifstream(const _Path& __s,
580 ios_base::openmode __mode = ios_base::in)
581 : basic_ifstream(__s.c_str(), __mode)
585 basic_ifstream(const basic_ifstream&) = delete;
587 basic_ifstream(basic_ifstream&& __rhs)
588 : __istream_type(std::move(__rhs)),
589 _M_filebuf(std::move(__rhs._M_filebuf))
590 { __istream_type::set_rdbuf(&_M_filebuf); }
594 * @brief The destructor does nothing.
596 * The file is closed by the filebuf object, not the formatting
602 #if __cplusplus >= 201103L
603 // 27.8.3.2 Assign and swap:
606 operator=(const basic_ifstream&) = delete;
609 operator=(basic_ifstream&& __rhs)
611 __istream_type::operator=(std::move(__rhs));
612 _M_filebuf = std::move(__rhs._M_filebuf);
617 swap(basic_ifstream& __rhs)
619 __istream_type::swap(__rhs);
620 _M_filebuf.swap(__rhs._M_filebuf);
626 * @brief Accessing the underlying buffer.
627 * @return The current basic_filebuf buffer.
629 * This hides both signatures of std::basic_ios::rdbuf().
633 { return const_cast<__filebuf_type*>(&_M_filebuf); }
636 * @brief Wrapper to test for an open file.
637 * @return @c rdbuf()->is_open()
641 { return _M_filebuf.is_open(); }
643 // _GLIBCXX_RESOLVE_LIB_DEFECTS
644 // 365. Lack of const-qualification in clause 27
647 { return _M_filebuf.is_open(); }
650 * @brief Opens an external file.
651 * @param __s The name of the file.
652 * @param __mode The open mode flags.
654 * Calls @c std::basic_filebuf::open(s,__mode|in). If that function
655 * fails, @c failbit is set in the stream's error state.
658 open(const char* __s, ios_base::openmode __mode = ios_base::in)
660 if (!_M_filebuf.open(__s, __mode | ios_base::in))
661 this->setstate(ios_base::failbit);
663 // _GLIBCXX_RESOLVE_LIB_DEFECTS
664 // 409. Closing an fstream should clear error state
668 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
670 * @brief Opens an external file.
671 * @param __s The name of the file, as a wide character string.
672 * @param __mode The open mode flags.
674 * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
675 * fails, @c failbit is set in the stream's error state.
678 open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in)
680 if (!_M_filebuf.open(__s, __mode | ios_base::in))
681 this->setstate(ios_base::failbit);
687 #if __cplusplus >= 201103L
689 * @brief Opens an external file.
690 * @param __s The name of the file.
691 * @param __mode The open mode flags.
693 * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
694 * fails, @c failbit is set in the stream's error state.
697 open(const std::string& __s, ios_base::openmode __mode = ios_base::in)
699 if (!_M_filebuf.open(__s, __mode | ios_base::in))
700 this->setstate(ios_base::failbit);
702 // _GLIBCXX_RESOLVE_LIB_DEFECTS
703 // 409. Closing an fstream should clear error state
707 #if __cplusplus >= 201703L
709 * @brief Opens an external file.
710 * @param __s The name of the file, as a filesystem::path.
711 * @param __mode The open mode flags.
713 * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
714 * fails, @c failbit is set in the stream's error state.
716 template<typename _Path>
717 _If_fs_path<_Path, void>
718 open(const _Path& __s, ios_base::openmode __mode = ios_base::in)
719 { open(__s.c_str(), __mode); }
724 * @brief Close the file.
726 * Calls @c std::basic_filebuf::close(). If that function
727 * fails, @c failbit is set in the stream's error state.
732 if (!_M_filebuf.close())
733 this->setstate(ios_base::failbit);
738 // [27.8.1.8] Template class basic_ofstream
740 * @brief Controlling output for files.
743 * @tparam _CharT Type of character stream.
744 * @tparam _Traits Traits for character type, defaults to
745 * char_traits<_CharT>.
747 * This class supports reading from named files, using the inherited
748 * functions from std::basic_ostream. To control the associated
749 * sequence, an instance of std::basic_filebuf is used, which this page
750 * refers to as @c sb.
752 template<typename _CharT, typename _Traits>
753 class basic_ofstream : public basic_ostream<_CharT,_Traits>
757 typedef _CharT char_type;
758 typedef _Traits traits_type;
759 typedef typename traits_type::int_type int_type;
760 typedef typename traits_type::pos_type pos_type;
761 typedef typename traits_type::off_type off_type;
763 // Non-standard types:
764 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
765 typedef basic_ostream<char_type, traits_type> __ostream_type;
768 __filebuf_type _M_filebuf;
773 * @brief Default constructor.
775 * Initializes @c sb using its default constructor, and passes
776 * @c &sb to the base class initializer. Does not open any files
777 * (you haven't given it a filename to open).
779 basic_ofstream(): __ostream_type(), _M_filebuf()
780 { this->init(&_M_filebuf); }
783 * @brief Create an output file stream.
784 * @param __s Null terminated string specifying the filename.
785 * @param __mode Open file in specified mode (see std::ios_base).
787 * @c ios_base::out is automatically included in @a __mode.
790 basic_ofstream(const char* __s,
791 ios_base::openmode __mode = ios_base::out)
792 : __ostream_type(), _M_filebuf()
794 this->init(&_M_filebuf);
795 this->open(__s, __mode);
798 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
800 * @param Create an output file stream.
801 * @param __s Wide string specifying the filename.
802 * @param __mode Open file in specified mode (see std::ios_base).
804 * @c ios_base::out | @c ios_base::trunc is automatically included in
807 basic_ofstream(const wchar_t* __s,
808 ios_base::openmode __mode = ios_base::out|ios_base::trunc)
809 : __ostream_type(), _M_filebuf()
811 this->init(&_M_filebuf);
812 this->open(__s, __mode);
816 #if __cplusplus >= 201103L
818 * @brief Create an output file stream.
819 * @param __s std::string specifying the filename.
820 * @param __mode Open file in specified mode (see std::ios_base).
822 * @c ios_base::out is automatically included in @a __mode.
825 basic_ofstream(const std::string& __s,
826 ios_base::openmode __mode = ios_base::out)
827 : __ostream_type(), _M_filebuf()
829 this->init(&_M_filebuf);
830 this->open(__s, __mode);
833 #if __cplusplus >= 201703L
835 * @param Create an output file stream.
836 * @param __s filesystem::path specifying the filename.
837 * @param __mode Open file in specified mode (see std::ios_base).
839 * @c ios_base::out is automatically included in @a __mode.
841 template<typename _Path, typename _Require = _If_fs_path<_Path>>
842 basic_ofstream(const _Path& __s,
843 ios_base::openmode __mode = ios_base::out)
844 : basic_ofstream(__s.c_str(), __mode)
848 basic_ofstream(const basic_ofstream&) = delete;
850 basic_ofstream(basic_ofstream&& __rhs)
851 : __ostream_type(std::move(__rhs)),
852 _M_filebuf(std::move(__rhs._M_filebuf))
853 { __ostream_type::set_rdbuf(&_M_filebuf); }
857 * @brief The destructor does nothing.
859 * The file is closed by the filebuf object, not the formatting
865 #if __cplusplus >= 201103L
866 // 27.8.3.2 Assign and swap:
869 operator=(const basic_ofstream&) = delete;
872 operator=(basic_ofstream&& __rhs)
874 __ostream_type::operator=(std::move(__rhs));
875 _M_filebuf = std::move(__rhs._M_filebuf);
880 swap(basic_ofstream& __rhs)
882 __ostream_type::swap(__rhs);
883 _M_filebuf.swap(__rhs._M_filebuf);
889 * @brief Accessing the underlying buffer.
890 * @return The current basic_filebuf buffer.
892 * This hides both signatures of std::basic_ios::rdbuf().
896 { return const_cast<__filebuf_type*>(&_M_filebuf); }
899 * @brief Wrapper to test for an open file.
900 * @return @c rdbuf()->is_open()
904 { return _M_filebuf.is_open(); }
906 // _GLIBCXX_RESOLVE_LIB_DEFECTS
907 // 365. Lack of const-qualification in clause 27
910 { return _M_filebuf.is_open(); }
913 * @brief Opens an external file.
914 * @param __s The name of the file.
915 * @param __mode The open mode flags.
917 * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
918 * function fails, @c failbit is set in the stream's error state.
921 open(const char* __s, ios_base::openmode __mode = ios_base::out)
923 if (!_M_filebuf.open(__s, __mode | ios_base::out))
924 this->setstate(ios_base::failbit);
926 // _GLIBCXX_RESOLVE_LIB_DEFECTS
927 // 409. Closing an fstream should clear error state
931 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
933 * @brief Opens an external file.
934 * @param __s The name of the file.
935 * @param __mode The open mode flags.
937 * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
938 * function fails, @c failbit is set in the stream's error state.
941 open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out)
943 if (!_M_filebuf.open(__s, __mode | ios_base::out))
944 this->setstate(ios_base::failbit);
950 #if __cplusplus >= 201103L
952 * @brief Opens an external file.
953 * @param __s The name of the file.
954 * @param __mode The open mode flags.
956 * Calls @c std::basic_filebuf::open(s,mode|out). If that
957 * function fails, @c failbit is set in the stream's error state.
960 open(const std::string& __s, ios_base::openmode __mode = ios_base::out)
962 if (!_M_filebuf.open(__s, __mode | ios_base::out))
963 this->setstate(ios_base::failbit);
965 // _GLIBCXX_RESOLVE_LIB_DEFECTS
966 // 409. Closing an fstream should clear error state
970 #if __cplusplus >= 201703L
972 * @brief Opens an external file.
973 * @param __s The name of the file, as a filesystem::path.
974 * @param __mode The open mode flags.
976 * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
977 * function fails, @c failbit is set in the stream's error state.
979 template<typename _Path>
980 _If_fs_path<_Path, void>
981 open(const _Path& __s, ios_base::openmode __mode = ios_base::out)
982 { open(__s.c_str(), __mode); }
987 * @brief Close the file.
989 * Calls @c std::basic_filebuf::close(). If that function
990 * fails, @c failbit is set in the stream's error state.
995 if (!_M_filebuf.close())
996 this->setstate(ios_base::failbit);
1001 // [27.8.1.11] Template class basic_fstream
1003 * @brief Controlling input and output for files.
1006 * @tparam _CharT Type of character stream.
1007 * @tparam _Traits Traits for character type, defaults to
1008 * char_traits<_CharT>.
1010 * This class supports reading from and writing to named files, using
1011 * the inherited functions from std::basic_iostream. To control the
1012 * associated sequence, an instance of std::basic_filebuf is used, which
1013 * this page refers to as @c sb.
1015 template<typename _CharT, typename _Traits>
1016 class basic_fstream : public basic_iostream<_CharT, _Traits>
1020 typedef _CharT char_type;
1021 typedef _Traits traits_type;
1022 typedef typename traits_type::int_type int_type;
1023 typedef typename traits_type::pos_type pos_type;
1024 typedef typename traits_type::off_type off_type;
1026 // Non-standard types:
1027 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
1028 typedef basic_ios<char_type, traits_type> __ios_type;
1029 typedef basic_iostream<char_type, traits_type> __iostream_type;
1032 __filebuf_type _M_filebuf;
1035 // Constructors/destructor:
1037 * @brief Default constructor.
1039 * Initializes @c sb using its default constructor, and passes
1040 * @c &sb to the base class initializer. Does not open any files
1041 * (you haven't given it a filename to open).
1044 : __iostream_type(), _M_filebuf()
1045 { this->init(&_M_filebuf); }
1048 * @brief Create an input/output file stream.
1049 * @param __s Null terminated string specifying the filename.
1050 * @param __mode Open file in specified mode (see std::ios_base).
1053 basic_fstream(const char* __s,
1054 ios_base::openmode __mode = ios_base::in | ios_base::out)
1055 : __iostream_type(0), _M_filebuf()
1057 this->init(&_M_filebuf);
1058 this->open(__s, __mode);
1061 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
1063 * @param Create an input/output file stream.
1064 * @param __s Wide string specifying the filename.
1065 * @param __mode Open file in specified mode (see std::ios_base).
1067 basic_fstream(const wchar_t* __s,
1068 ios_base::openmode __mode = ios_base::in | ios_base::out)
1069 : __iostream_type(0), _M_filebuf()
1071 this->init(&_M_filebuf);
1072 this->open(__s, __mode);
1076 #if __cplusplus >= 201103L
1078 * @brief Create an input/output file stream.
1079 * @param __s Null terminated string specifying the filename.
1080 * @param __mode Open file in specified mode (see std::ios_base).
1083 basic_fstream(const std::string& __s,
1084 ios_base::openmode __mode = ios_base::in | ios_base::out)
1085 : __iostream_type(0), _M_filebuf()
1087 this->init(&_M_filebuf);
1088 this->open(__s, __mode);
1091 #if __cplusplus >= 201703L
1093 * @param Create an input/output file stream.
1094 * @param __s filesystem::path specifying the filename.
1095 * @param __mode Open file in specified mode (see std::ios_base).
1097 template<typename _Path, typename _Require = _If_fs_path<_Path>>
1098 basic_fstream(const _Path& __s,
1099 ios_base::openmode __mode = ios_base::in | ios_base::out)
1100 : basic_fstream(__s.c_str(), __mode)
1104 basic_fstream(const basic_fstream&) = delete;
1106 basic_fstream(basic_fstream&& __rhs)
1107 : __iostream_type(std::move(__rhs)),
1108 _M_filebuf(std::move(__rhs._M_filebuf))
1109 { __iostream_type::set_rdbuf(&_M_filebuf); }
1113 * @brief The destructor does nothing.
1115 * The file is closed by the filebuf object, not the formatting
1121 #if __cplusplus >= 201103L
1122 // 27.8.3.2 Assign and swap:
1125 operator=(const basic_fstream&) = delete;
1128 operator=(basic_fstream&& __rhs)
1130 __iostream_type::operator=(std::move(__rhs));
1131 _M_filebuf = std::move(__rhs._M_filebuf);
1136 swap(basic_fstream& __rhs)
1138 __iostream_type::swap(__rhs);
1139 _M_filebuf.swap(__rhs._M_filebuf);
1145 * @brief Accessing the underlying buffer.
1146 * @return The current basic_filebuf buffer.
1148 * This hides both signatures of std::basic_ios::rdbuf().
1152 { return const_cast<__filebuf_type*>(&_M_filebuf); }
1155 * @brief Wrapper to test for an open file.
1156 * @return @c rdbuf()->is_open()
1160 { return _M_filebuf.is_open(); }
1162 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1163 // 365. Lack of const-qualification in clause 27
1166 { return _M_filebuf.is_open(); }
1169 * @brief Opens an external file.
1170 * @param __s The name of the file.
1171 * @param __mode The open mode flags.
1173 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1174 * function fails, @c failbit is set in the stream's error state.
1177 open(const char* __s,
1178 ios_base::openmode __mode = ios_base::in | ios_base::out)
1180 if (!_M_filebuf.open(__s, __mode))
1181 this->setstate(ios_base::failbit);
1183 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1184 // 409. Closing an fstream should clear error state
1188 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
1190 * @brief Opens an external file.
1191 * @param __s The name of the file.
1192 * @param __mode The open mode flags.
1194 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1195 * function fails, @c failbit is set in the stream's error state.
1198 open(const wchar_t* __s,
1199 ios_base::openmode __mode = ios_base::in | ios_base::out)
1201 if (!_M_filebuf.open(__s, __mode))
1202 this->setstate(ios_base::failbit);
1208 #if __cplusplus >= 201103L
1210 * @brief Opens an external file.
1211 * @param __s The name of the file.
1212 * @param __mode The open mode flags.
1214 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1215 * function fails, @c failbit is set in the stream's error state.
1218 open(const std::string& __s,
1219 ios_base::openmode __mode = ios_base::in | ios_base::out)
1221 if (!_M_filebuf.open(__s, __mode))
1222 this->setstate(ios_base::failbit);
1224 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1225 // 409. Closing an fstream should clear error state
1229 #if __cplusplus >= 201703L
1231 * @brief Opens an external file.
1232 * @param __s The name of the file, as a filesystem::path.
1233 * @param __mode The open mode flags.
1235 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1236 * function fails, @c failbit is set in the stream's error state.
1238 template<typename _Path>
1239 _If_fs_path<_Path, void>
1240 open(const _Path& __s,
1241 ios_base::openmode __mode = ios_base::in | ios_base::out)
1242 { open(__s.c_str(), __mode); }
1247 * @brief Close the file.
1249 * Calls @c std::basic_filebuf::close(). If that function
1250 * fails, @c failbit is set in the stream's error state.
1255 if (!_M_filebuf.close())
1256 this->setstate(ios_base::failbit);
1260 #if __cplusplus >= 201103L
1261 /// Swap specialization for filebufs.
1262 template <class _CharT, class _Traits>
1264 swap(basic_filebuf<_CharT, _Traits>& __x,
1265 basic_filebuf<_CharT, _Traits>& __y)
1268 /// Swap specialization for ifstreams.
1269 template <class _CharT, class _Traits>
1271 swap(basic_ifstream<_CharT, _Traits>& __x,
1272 basic_ifstream<_CharT, _Traits>& __y)
1275 /// Swap specialization for ofstreams.
1276 template <class _CharT, class _Traits>
1278 swap(basic_ofstream<_CharT, _Traits>& __x,
1279 basic_ofstream<_CharT, _Traits>& __y)
1282 /// Swap specialization for fstreams.
1283 template <class _CharT, class _Traits>
1285 swap(basic_fstream<_CharT, _Traits>& __x,
1286 basic_fstream<_CharT, _Traits>& __y)
1290 _GLIBCXX_END_NAMESPACE_VERSION
1293 #include <bits/fstream.tcc>
1295 #endif /* _GLIBCXX_FSTREAM */