1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997-2024 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
38 #include <bits/requires_hosted.h> // iostreams
42 #include <bits/codecvt.h>
43 #include <cstdio> // For BUFSIZ
44 #include <bits/basic_file.h> // For __basic_file, __c_lock
45 #if __cplusplus >= 201103L
46 #include <string> // For std::string overloads.
49 #define __glibcxx_want_fstream_native_handle
50 #include <bits/version.h>
52 // This can be overridden by the target's os_defines.h
53 #ifndef _GLIBCXX_BUFSIZ
54 # define _GLIBCXX_BUFSIZ BUFSIZ
57 namespace std _GLIBCXX_VISIBILITY(default)
59 _GLIBCXX_BEGIN_NAMESPACE_VERSION
61 #if __cplusplus >= 201703L
62 // Enable if _Path is a filesystem::path or experimental::filesystem::path
63 template<typename _Path, typename _Result = _Path, typename _Path2
64 = decltype(std::declval<_Path&>().make_preferred().filename())>
65 using _If_fs_path = enable_if_t<is_same_v<_Path, _Path2>, _Result>;
69 // [27.8.1.1] template class basic_filebuf
71 * @brief The actual work of input and output (for files).
74 * @tparam _CharT Type of character stream.
75 * @tparam _Traits Traits for character type, defaults to
76 * char_traits<_CharT>.
78 * This class associates both its input and output sequence with an
79 * external disk file, and maintains a joint file position for both
80 * sequences. Many of its semantics are described in terms of similar
81 * behavior in the Standard C Library's @c FILE streams.
83 * Requirements on traits_type, specific to this class:
84 * - traits_type::pos_type must be fpos<traits_type::state_type>
85 * - traits_type::off_type must be streamoff
86 * - traits_type::state_type must be Assignable and DefaultConstructible,
87 * - traits_type::state_type() must be the initial state for codecvt.
89 template<typename _CharT, typename _Traits>
90 class basic_filebuf : public basic_streambuf<_CharT, _Traits>
92 #if __cplusplus >= 201103L
93 template<typename _Tp>
94 using __chk_state = __and_<is_copy_assignable<_Tp>,
95 is_copy_constructible<_Tp>,
96 is_default_constructible<_Tp>>;
98 static_assert(__chk_state<typename _Traits::state_type>::value,
99 "state_type must be CopyAssignable, CopyConstructible"
100 " and DefaultConstructible");
102 static_assert(is_same<typename _Traits::pos_type,
103 fpos<typename _Traits::state_type>>::value,
104 "pos_type must be fpos<state_type>");
108 typedef _CharT char_type;
109 typedef _Traits traits_type;
110 typedef typename traits_type::int_type int_type;
111 typedef typename traits_type::pos_type pos_type;
112 typedef typename traits_type::off_type off_type;
114 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
115 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
116 typedef __basic_file<char> __file_type;
117 typedef typename traits_type::state_type __state_type;
118 typedef codecvt<char_type, char, __state_type> __codecvt_type;
120 friend class ios_base; // For sync_with_stdio.
124 // MT lock inherited from libio or other low-level io library.
130 /// Place to stash in || out || in | out settings for current filebuf.
131 ios_base::openmode _M_mode;
133 // Beginning state type for codecvt.
134 __state_type _M_state_beg;
136 // During output, the state that corresponds to pptr(),
137 // during input, the state that corresponds to egptr() and
139 __state_type _M_state_cur;
141 // Not used for output. During input, the state that corresponds
142 // to eback() and _M_ext_buf.
143 __state_type _M_state_last;
145 /// Pointer to the beginning of internal buffer.
149 * Actual size of internal buffer. This number is equal to the size
150 * of the put area + 1 position, reserved for the overflow char of
155 // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
156 bool _M_buf_allocated;
159 * _M_reading == false && _M_writing == false for @b uncommitted mode;
160 * _M_reading == true for @b read mode;
161 * _M_writing == true for @b write mode;
163 * NB: _M_reading == true && _M_writing == true is unused.
170 * Necessary bits for putback buffer management.
172 * @note pbacks of over one character are not currently supported.
175 char_type* _M_pback_cur_save;
176 char_type* _M_pback_end_save;
180 // Cached codecvt facet.
181 const __codecvt_type* _M_codecvt;
184 * Buffer for external characters. Used for input when
185 * codecvt::always_noconv() == false. When valid, this corresponds
191 * Size of buffer held by _M_ext_buf.
193 streamsize _M_ext_buf_size;
196 * Pointers into the buffer held by _M_ext_buf that delimit a
197 * subsequence of bytes that have been read but not yet converted.
198 * When valid, _M_ext_next corresponds to egptr().
200 const char* _M_ext_next;
204 * Initializes pback buffers, and moves normal buffers to safety.
206 * _M_in_cur has already been moved back
213 _M_pback_cur_save = this->gptr();
214 _M_pback_end_save = this->egptr();
215 this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
216 _M_pback_init = true;
221 * Deactivates pback buffer contents, and restores normal buffer.
223 * The pback buffer has only moved forward.
226 _M_destroy_pback() throw()
230 // Length _M_in_cur moved in the pback buffer.
231 _M_pback_cur_save += this->gptr() != this->eback();
232 this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
233 _M_pback_init = false;
238 // Constructors/destructor:
240 * @brief Does not open any files.
242 * The default constructor initializes the parent class using its
247 #if __cplusplus >= 201103L
248 basic_filebuf(const basic_filebuf&) = delete;
249 basic_filebuf(basic_filebuf&&);
253 * @brief The destructor closes the file first.
264 #if __cplusplus >= 201103L
265 basic_filebuf& operator=(const basic_filebuf&) = delete;
266 basic_filebuf& operator=(basic_filebuf&&);
267 void swap(basic_filebuf&);
272 * @brief Returns true if the external file is open.
275 is_open() const throw()
276 { return _M_file.is_open(); }
279 * @brief Opens an external file.
280 * @param __s The name of the file.
281 * @param __mode The open mode flags.
282 * @return @c this on success, NULL on failure
284 * If a file is already open, this function immediately fails.
285 * Otherwise it tries to open the file named @a __s using the flags
286 * given in @a __mode.
288 * Table 92, adapted here, gives the relation between openmode
289 * combinations and the equivalent @c fopen() flags.
290 * (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,
291 * and binary|in|app per DR 596)
293 * +---------------------------------------------------------+
294 * | ios_base Flag combination stdio equivalent |
295 * |binary in out trunc app |
296 * +---------------------------------------------------------+
306 * +---------------------------------------------------------+
316 * +---------------------------------------------------------+
320 open(const char* __s, ios_base::openmode __mode);
322 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
324 * @brief Opens an external file.
325 * @param __s The name of the file, as a wide character string.
326 * @param __mode The open mode flags.
327 * @return @c this on success, NULL on failure
330 open(const wchar_t* __s, ios_base::openmode __mode);
333 #if __cplusplus >= 201103L
335 * @brief Opens an external file.
336 * @param __s The name of the file.
337 * @param __mode The open mode flags.
338 * @return @c this on success, NULL on failure
341 open(const std::string& __s, ios_base::openmode __mode)
342 { return open(__s.c_str(), __mode); }
344 #if __cplusplus >= 201703L
346 * @brief Opens an external file.
347 * @param __s The name of the file, as a filesystem::path.
348 * @param __mode The open mode flags.
349 * @return @c this on success, NULL on failure
351 template<typename _Path>
352 _If_fs_path<_Path, __filebuf_type*>
353 open(const _Path& __s, ios_base::openmode __mode)
354 { return open(__s.c_str(), __mode); }
359 * @brief Closes the currently associated file.
360 * @return @c this on success, NULL on failure
362 * If no file is currently open, this function immediately fails.
364 * If a <em>put buffer area</em> exists, @c overflow(eof) is
365 * called to flush all the characters. The file is then
368 * If any operations fail, this function also fails.
373 #if __cpp_lib_fstream_native_handle // C++ >= 26
375 * @brief The platform-specific file handle type.
377 * The type is `int` for POSIX platforms that use file descriptors,
378 * or `HANDLE` for Windows, or `FILE*` if the library was configured
379 * with `--enable-cstdio=stdio_pure`.
383 using native_handle_type = typename __file_type::native_handle_type;
386 * @brief Return the platform-specific native handle for the file.
387 * @pre `is_open()` is true.
388 * @return The native file handle associated with `*this`.
390 * The handle is invalidated when this filebuf is closed or destroyed.
394 [[__gnu__::__always_inline__]]
396 native_handle() const noexcept
398 __glibcxx_assert(is_open());
399 return _M_file.native_handle();
405 _M_allocate_internal_buffer();
408 _M_destroy_internal_buffer() throw();
410 // [27.8.1.4] overridden virtual functions
414 // Stroustrup, 1998, p. 628
415 // underflow() and uflow() functions are called to get the next
416 // character from the real input source when the buffer is empty.
417 // Buffered input uses underflow()
423 pbackfail(int_type __c = _Traits::eof());
425 // Stroustrup, 1998, p 648
426 // The overflow() function is called to transfer characters to the
427 // real output destination when the buffer is full. A call to
428 // overflow(c) outputs the contents of the buffer plus the
431 // Consume some sequence of the characters in the pending sequence.
433 overflow(int_type __c = _Traits::eof());
435 // Convert internal byte sequence to external, char-based
436 // sequence via codecvt.
438 _M_convert_to_external(char_type*, streamsize);
441 * @brief Manipulates the buffer.
442 * @param __s Pointer to a buffer area.
443 * @param __n Size of @a __s.
446 * If no file has been opened, and both @a __s and @a __n are zero, then
447 * the stream becomes unbuffered. Otherwise, @c __s is used as a
449 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
452 virtual __streambuf_type*
453 setbuf(char_type* __s, streamsize __n);
456 seekoff(off_type __off, ios_base::seekdir __way,
457 ios_base::openmode __mode = ios_base::in | ios_base::out);
460 seekpos(pos_type __pos,
461 ios_base::openmode __mode = ios_base::in | ios_base::out);
463 // Common code for seekoff, seekpos, and overflow
465 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
468 _M_get_ext_pos(__state_type &__state);
474 imbue(const locale& __loc);
477 xsgetn(char_type* __s, streamsize __n);
480 xsputn(const char_type* __s, streamsize __n);
482 // Flushes output buffer, then writes unshift sequence.
484 _M_terminate_output();
487 * This function sets the pointers of the internal buffer, both get
488 * and put areas. Typically:
490 * __off == egptr() - eback() upon underflow/uflow (@b read mode);
491 * __off == 0 upon overflow (@b write mode);
492 * __off == -1 upon open, setbuf, seekoff/pos (@b uncommitted mode).
494 * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
495 * reflects the actual allocated memory and the last cell is reserved
496 * for the overflow char of a full put area.
499 _M_set_buffer(streamsize __off)
501 const bool __testin = _M_mode & ios_base::in;
502 const bool __testout = (_M_mode & ios_base::out
503 || _M_mode & ios_base::app);
505 if (__testin && __off > 0)
506 this->setg(_M_buf, _M_buf, _M_buf + __off);
508 this->setg(_M_buf, _M_buf, _M_buf);
510 if (__testout && __off == 0 && _M_buf_size > 1 )
511 this->setp(_M_buf, _M_buf + _M_buf_size - 1);
517 // [27.8.1.5] Template class basic_ifstream
519 * @brief Controlling input for files.
522 * @tparam _CharT Type of character stream.
523 * @tparam _Traits Traits for character type, defaults to
524 * char_traits<_CharT>.
526 * This class supports reading from named files, using the inherited
527 * functions from std::basic_istream. To control the associated
528 * sequence, an instance of std::basic_filebuf is used, which this page
529 * refers to as @c sb.
531 template<typename _CharT, typename _Traits>
532 class basic_ifstream : public basic_istream<_CharT, _Traits>
536 typedef _CharT char_type;
537 typedef _Traits traits_type;
538 typedef typename traits_type::int_type int_type;
539 typedef typename traits_type::pos_type pos_type;
540 typedef typename traits_type::off_type off_type;
542 // Non-standard types:
543 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
544 typedef basic_istream<char_type, traits_type> __istream_type;
547 __filebuf_type _M_filebuf;
550 // Constructors/Destructors:
552 * @brief Default constructor.
554 * Initializes @c sb using its default constructor, and passes
555 * @c &sb to the base class initializer. Does not open any files
556 * (you haven't given it a filename to open).
558 basic_ifstream() : __istream_type(), _M_filebuf()
559 { this->init(&_M_filebuf); }
562 * @brief Create an input file stream.
563 * @param __s Null terminated string specifying the filename.
564 * @param __mode Open file in specified mode (see std::ios_base).
566 * @c ios_base::in is automatically included in @a __mode.
569 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
570 : __istream_type(), _M_filebuf()
572 this->init(&_M_filebuf);
573 this->open(__s, __mode);
576 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
578 * @param Create an input file stream.
579 * @param __s Wide string specifying the filename.
580 * @param __mode Open file in specified mode (see std::ios_base).
582 * @c ios_base::in is automatically included in @a __mode.
584 basic_ifstream(const wchar_t* __s,
585 ios_base::openmode __mode = ios_base::in)
586 : __istream_type(), _M_filebuf()
588 this->init(&_M_filebuf);
589 this->open(__s, __mode);
593 #if __cplusplus >= 201103L
595 * @brief Create an input file stream.
596 * @param __s std::string specifying the filename.
597 * @param __mode Open file in specified mode (see std::ios_base).
599 * @c ios_base::in is automatically included in @a __mode.
602 basic_ifstream(const std::string& __s,
603 ios_base::openmode __mode = ios_base::in)
604 : __istream_type(), _M_filebuf()
606 this->init(&_M_filebuf);
607 this->open(__s, __mode);
610 #if __cplusplus >= 201703L
612 * @brief Create an input file stream.
613 * @param __s filesystem::path specifying the filename.
614 * @param __mode Open file in specified mode (see std::ios_base).
616 * @c ios_base::in is automatically included in @a __mode.
618 template<typename _Path, typename _Require = _If_fs_path<_Path>>
619 basic_ifstream(const _Path& __s,
620 ios_base::openmode __mode = ios_base::in)
621 : basic_ifstream(__s.c_str(), __mode)
625 basic_ifstream(const basic_ifstream&) = delete;
627 basic_ifstream(basic_ifstream&& __rhs)
628 : __istream_type(std::move(__rhs)),
629 _M_filebuf(std::move(__rhs._M_filebuf))
630 { __istream_type::set_rdbuf(&_M_filebuf); }
634 * @brief The destructor does nothing.
636 * The file is closed by the filebuf object, not the formatting
642 #if __cplusplus >= 201103L
643 // 27.8.3.2 Assign and swap:
646 operator=(const basic_ifstream&) = delete;
649 operator=(basic_ifstream&& __rhs)
651 __istream_type::operator=(std::move(__rhs));
652 _M_filebuf = std::move(__rhs._M_filebuf);
657 swap(basic_ifstream& __rhs)
659 __istream_type::swap(__rhs);
660 _M_filebuf.swap(__rhs._M_filebuf);
666 * @brief Accessing the underlying buffer.
667 * @return The current basic_filebuf buffer.
669 * This hides both signatures of std::basic_ios::rdbuf().
673 { return const_cast<__filebuf_type*>(&_M_filebuf); }
676 * @brief Wrapper to test for an open file.
677 * @return @c rdbuf()->is_open()
681 { return _M_filebuf.is_open(); }
683 // _GLIBCXX_RESOLVE_LIB_DEFECTS
684 // 365. Lack of const-qualification in clause 27
687 { return _M_filebuf.is_open(); }
690 * @brief Opens an external file.
691 * @param __s The name of the file.
692 * @param __mode The open mode flags.
694 * Calls @c std::basic_filebuf::open(s,__mode|in). If that function
695 * fails, @c failbit is set in the stream's error state.
698 open(const char* __s, ios_base::openmode __mode = ios_base::in)
700 if (!_M_filebuf.open(__s, __mode | ios_base::in))
701 this->setstate(ios_base::failbit);
703 // _GLIBCXX_RESOLVE_LIB_DEFECTS
704 // 409. Closing an fstream should clear error state
708 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
710 * @brief Opens an external file.
711 * @param __s The name of the file, as a wide character string.
712 * @param __mode The open mode flags.
714 * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
715 * fails, @c failbit is set in the stream's error state.
718 open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in)
720 if (!_M_filebuf.open(__s, __mode | ios_base::in))
721 this->setstate(ios_base::failbit);
727 #if __cplusplus >= 201103L
729 * @brief Opens an external file.
730 * @param __s The name of the file.
731 * @param __mode The open mode flags.
733 * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
734 * fails, @c failbit is set in the stream's error state.
737 open(const std::string& __s, ios_base::openmode __mode = ios_base::in)
739 if (!_M_filebuf.open(__s, __mode | ios_base::in))
740 this->setstate(ios_base::failbit);
742 // _GLIBCXX_RESOLVE_LIB_DEFECTS
743 // 409. Closing an fstream should clear error state
747 #if __cplusplus >= 201703L
749 * @brief Opens an external file.
750 * @param __s The name of the file, as a filesystem::path.
751 * @param __mode The open mode flags.
753 * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
754 * fails, @c failbit is set in the stream's error state.
756 template<typename _Path>
757 _If_fs_path<_Path, void>
758 open(const _Path& __s, ios_base::openmode __mode = ios_base::in)
759 { open(__s.c_str(), __mode); }
764 * @brief Close the file.
766 * Calls @c std::basic_filebuf::close(). If that function
767 * fails, @c failbit is set in the stream's error state.
772 if (!_M_filebuf.close())
773 this->setstate(ios_base::failbit);
776 #if __cpp_lib_fstream_native_handle // C++ >= 26
777 using native_handle_type = typename __filebuf_type::native_handle_type;
779 [[__gnu__::__always_inline__]]
781 native_handle() const noexcept
782 { return _M_filebuf.native_handle(); }
787 // [27.8.1.8] Template class basic_ofstream
789 * @brief Controlling output for files.
792 * @tparam _CharT Type of character stream.
793 * @tparam _Traits Traits for character type, defaults to
794 * char_traits<_CharT>.
796 * This class supports reading from named files, using the inherited
797 * functions from std::basic_ostream. To control the associated
798 * sequence, an instance of std::basic_filebuf is used, which this page
799 * refers to as @c sb.
801 template<typename _CharT, typename _Traits>
802 class basic_ofstream : public basic_ostream<_CharT,_Traits>
806 typedef _CharT char_type;
807 typedef _Traits traits_type;
808 typedef typename traits_type::int_type int_type;
809 typedef typename traits_type::pos_type pos_type;
810 typedef typename traits_type::off_type off_type;
812 // Non-standard types:
813 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
814 typedef basic_ostream<char_type, traits_type> __ostream_type;
817 __filebuf_type _M_filebuf;
822 * @brief Default constructor.
824 * Initializes @c sb using its default constructor, and passes
825 * @c &sb to the base class initializer. Does not open any files
826 * (you haven't given it a filename to open).
828 basic_ofstream(): __ostream_type(), _M_filebuf()
829 { this->init(&_M_filebuf); }
832 * @brief Create an output file stream.
833 * @param __s Null terminated string specifying the filename.
834 * @param __mode Open file in specified mode (see std::ios_base).
836 * @c ios_base::out is automatically included in @a __mode.
839 basic_ofstream(const char* __s,
840 ios_base::openmode __mode = ios_base::out)
841 : __ostream_type(), _M_filebuf()
843 this->init(&_M_filebuf);
844 this->open(__s, __mode);
847 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
849 * @param Create an output file stream.
850 * @param __s Wide string specifying the filename.
851 * @param __mode Open file in specified mode (see std::ios_base).
853 * @c ios_base::out | @c ios_base::trunc is automatically included in
856 basic_ofstream(const wchar_t* __s,
857 ios_base::openmode __mode = ios_base::out|ios_base::trunc)
858 : __ostream_type(), _M_filebuf()
860 this->init(&_M_filebuf);
861 this->open(__s, __mode);
865 #if __cplusplus >= 201103L
867 * @brief Create an output file stream.
868 * @param __s std::string specifying the filename.
869 * @param __mode Open file in specified mode (see std::ios_base).
871 * @c ios_base::out is automatically included in @a __mode.
874 basic_ofstream(const std::string& __s,
875 ios_base::openmode __mode = ios_base::out)
876 : __ostream_type(), _M_filebuf()
878 this->init(&_M_filebuf);
879 this->open(__s, __mode);
882 #if __cplusplus >= 201703L
884 * @brief Create an output file stream.
885 * @param __s filesystem::path specifying the filename.
886 * @param __mode Open file in specified mode (see std::ios_base).
888 * @c ios_base::out is automatically included in @a __mode.
890 template<typename _Path, typename _Require = _If_fs_path<_Path>>
891 basic_ofstream(const _Path& __s,
892 ios_base::openmode __mode = ios_base::out)
893 : basic_ofstream(__s.c_str(), __mode)
897 basic_ofstream(const basic_ofstream&) = delete;
899 basic_ofstream(basic_ofstream&& __rhs)
900 : __ostream_type(std::move(__rhs)),
901 _M_filebuf(std::move(__rhs._M_filebuf))
902 { __ostream_type::set_rdbuf(&_M_filebuf); }
906 * @brief The destructor does nothing.
908 * The file is closed by the filebuf object, not the formatting
914 #if __cplusplus >= 201103L
915 // 27.8.3.2 Assign and swap:
918 operator=(const basic_ofstream&) = delete;
921 operator=(basic_ofstream&& __rhs)
923 __ostream_type::operator=(std::move(__rhs));
924 _M_filebuf = std::move(__rhs._M_filebuf);
929 swap(basic_ofstream& __rhs)
931 __ostream_type::swap(__rhs);
932 _M_filebuf.swap(__rhs._M_filebuf);
938 * @brief Accessing the underlying buffer.
939 * @return The current basic_filebuf buffer.
941 * This hides both signatures of std::basic_ios::rdbuf().
945 { return const_cast<__filebuf_type*>(&_M_filebuf); }
948 * @brief Wrapper to test for an open file.
949 * @return @c rdbuf()->is_open()
953 { return _M_filebuf.is_open(); }
955 // _GLIBCXX_RESOLVE_LIB_DEFECTS
956 // 365. Lack of const-qualification in clause 27
959 { return _M_filebuf.is_open(); }
962 * @brief Opens an external file.
963 * @param __s The name of the file.
964 * @param __mode The open mode flags.
966 * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
967 * function fails, @c failbit is set in the stream's error state.
970 open(const char* __s, ios_base::openmode __mode = ios_base::out)
972 if (!_M_filebuf.open(__s, __mode | ios_base::out))
973 this->setstate(ios_base::failbit);
975 // _GLIBCXX_RESOLVE_LIB_DEFECTS
976 // 409. Closing an fstream should clear error state
980 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
982 * @brief Opens an external file.
983 * @param __s The name of the file.
984 * @param __mode The open mode flags.
986 * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
987 * function fails, @c failbit is set in the stream's error state.
990 open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out)
992 if (!_M_filebuf.open(__s, __mode | ios_base::out))
993 this->setstate(ios_base::failbit);
999 #if __cplusplus >= 201103L
1001 * @brief Opens an external file.
1002 * @param __s The name of the file.
1003 * @param __mode The open mode flags.
1005 * Calls @c std::basic_filebuf::open(s,mode|out). If that
1006 * function fails, @c failbit is set in the stream's error state.
1009 open(const std::string& __s, ios_base::openmode __mode = ios_base::out)
1011 if (!_M_filebuf.open(__s, __mode | ios_base::out))
1012 this->setstate(ios_base::failbit);
1014 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1015 // 409. Closing an fstream should clear error state
1019 #if __cplusplus >= 201703L
1021 * @brief Opens an external file.
1022 * @param __s The name of the file, as a filesystem::path.
1023 * @param __mode The open mode flags.
1025 * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
1026 * function fails, @c failbit is set in the stream's error state.
1028 template<typename _Path>
1029 _If_fs_path<_Path, void>
1030 open(const _Path& __s, ios_base::openmode __mode = ios_base::out)
1031 { open(__s.c_str(), __mode); }
1036 * @brief Close the file.
1038 * Calls @c std::basic_filebuf::close(). If that function
1039 * fails, @c failbit is set in the stream's error state.
1044 if (!_M_filebuf.close())
1045 this->setstate(ios_base::failbit);
1048 #if __cpp_lib_fstream_native_handle // C++ >= 26
1049 using native_handle_type = typename __filebuf_type::native_handle_type;
1051 [[__gnu__::__always_inline__]]
1053 native_handle() const noexcept
1054 { return _M_filebuf.native_handle(); }
1059 // [27.8.1.11] Template class basic_fstream
1061 * @brief Controlling input and output for files.
1064 * @tparam _CharT Type of character stream.
1065 * @tparam _Traits Traits for character type, defaults to
1066 * char_traits<_CharT>.
1068 * This class supports reading from and writing to named files, using
1069 * the inherited functions from std::basic_iostream. To control the
1070 * associated sequence, an instance of std::basic_filebuf is used, which
1071 * this page refers to as @c sb.
1073 template<typename _CharT, typename _Traits>
1074 class basic_fstream : public basic_iostream<_CharT, _Traits>
1078 typedef _CharT char_type;
1079 typedef _Traits traits_type;
1080 typedef typename traits_type::int_type int_type;
1081 typedef typename traits_type::pos_type pos_type;
1082 typedef typename traits_type::off_type off_type;
1084 // Non-standard types:
1085 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
1086 typedef basic_ios<char_type, traits_type> __ios_type;
1087 typedef basic_iostream<char_type, traits_type> __iostream_type;
1090 __filebuf_type _M_filebuf;
1093 // Constructors/destructor:
1095 * @brief Default constructor.
1097 * Initializes @c sb using its default constructor, and passes
1098 * @c &sb to the base class initializer. Does not open any files
1099 * (you haven't given it a filename to open).
1102 : __iostream_type(), _M_filebuf()
1103 { this->init(&_M_filebuf); }
1106 * @brief Create an input/output file stream.
1107 * @param __s Null terminated string specifying the filename.
1108 * @param __mode Open file in specified mode (see std::ios_base).
1111 basic_fstream(const char* __s,
1112 ios_base::openmode __mode = ios_base::in | ios_base::out)
1113 : __iostream_type(0), _M_filebuf()
1115 this->init(&_M_filebuf);
1116 this->open(__s, __mode);
1119 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
1121 * @param Create an input/output file stream.
1122 * @param __s Wide string specifying the filename.
1123 * @param __mode Open file in specified mode (see std::ios_base).
1125 basic_fstream(const wchar_t* __s,
1126 ios_base::openmode __mode = ios_base::in | ios_base::out)
1127 : __iostream_type(0), _M_filebuf()
1129 this->init(&_M_filebuf);
1130 this->open(__s, __mode);
1134 #if __cplusplus >= 201103L
1136 * @brief Create an input/output file stream.
1137 * @param __s Null terminated string specifying the filename.
1138 * @param __mode Open file in specified mode (see std::ios_base).
1141 basic_fstream(const std::string& __s,
1142 ios_base::openmode __mode = ios_base::in | ios_base::out)
1143 : __iostream_type(0), _M_filebuf()
1145 this->init(&_M_filebuf);
1146 this->open(__s, __mode);
1149 #if __cplusplus >= 201703L
1151 * @brief Create an input/output file stream.
1152 * @param __s filesystem::path specifying the filename.
1153 * @param __mode Open file in specified mode (see std::ios_base).
1155 template<typename _Path, typename _Require = _If_fs_path<_Path>>
1156 basic_fstream(const _Path& __s,
1157 ios_base::openmode __mode = ios_base::in | ios_base::out)
1158 : basic_fstream(__s.c_str(), __mode)
1162 basic_fstream(const basic_fstream&) = delete;
1164 basic_fstream(basic_fstream&& __rhs)
1165 : __iostream_type(std::move(__rhs)),
1166 _M_filebuf(std::move(__rhs._M_filebuf))
1167 { __iostream_type::set_rdbuf(&_M_filebuf); }
1171 * @brief The destructor does nothing.
1173 * The file is closed by the filebuf object, not the formatting
1179 #if __cplusplus >= 201103L
1180 // 27.8.3.2 Assign and swap:
1183 operator=(const basic_fstream&) = delete;
1186 operator=(basic_fstream&& __rhs)
1188 __iostream_type::operator=(std::move(__rhs));
1189 _M_filebuf = std::move(__rhs._M_filebuf);
1194 swap(basic_fstream& __rhs)
1196 __iostream_type::swap(__rhs);
1197 _M_filebuf.swap(__rhs._M_filebuf);
1203 * @brief Accessing the underlying buffer.
1204 * @return The current basic_filebuf buffer.
1206 * This hides both signatures of std::basic_ios::rdbuf().
1210 { return const_cast<__filebuf_type*>(&_M_filebuf); }
1213 * @brief Wrapper to test for an open file.
1214 * @return @c rdbuf()->is_open()
1218 { return _M_filebuf.is_open(); }
1220 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1221 // 365. Lack of const-qualification in clause 27
1224 { return _M_filebuf.is_open(); }
1227 * @brief Opens an external file.
1228 * @param __s The name of the file.
1229 * @param __mode The open mode flags.
1231 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1232 * function fails, @c failbit is set in the stream's error state.
1235 open(const char* __s,
1236 ios_base::openmode __mode = ios_base::in | ios_base::out)
1238 if (!_M_filebuf.open(__s, __mode))
1239 this->setstate(ios_base::failbit);
1241 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1242 // 409. Closing an fstream should clear error state
1246 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
1248 * @brief Opens an external file.
1249 * @param __s The name of the file.
1250 * @param __mode The open mode flags.
1252 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1253 * function fails, @c failbit is set in the stream's error state.
1256 open(const wchar_t* __s,
1257 ios_base::openmode __mode = ios_base::in | ios_base::out)
1259 if (!_M_filebuf.open(__s, __mode))
1260 this->setstate(ios_base::failbit);
1266 #if __cplusplus >= 201103L
1268 * @brief Opens an external file.
1269 * @param __s The name of the file.
1270 * @param __mode The open mode flags.
1272 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1273 * function fails, @c failbit is set in the stream's error state.
1276 open(const std::string& __s,
1277 ios_base::openmode __mode = ios_base::in | ios_base::out)
1279 if (!_M_filebuf.open(__s, __mode))
1280 this->setstate(ios_base::failbit);
1282 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1283 // 409. Closing an fstream should clear error state
1287 #if __cplusplus >= 201703L
1289 * @brief Opens an external file.
1290 * @param __s The name of the file, as a filesystem::path.
1291 * @param __mode The open mode flags.
1293 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1294 * function fails, @c failbit is set in the stream's error state.
1296 template<typename _Path>
1297 _If_fs_path<_Path, void>
1298 open(const _Path& __s,
1299 ios_base::openmode __mode = ios_base::in | ios_base::out)
1300 { open(__s.c_str(), __mode); }
1305 * @brief Close the file.
1307 * Calls @c std::basic_filebuf::close(). If that function
1308 * fails, @c failbit is set in the stream's error state.
1313 if (!_M_filebuf.close())
1314 this->setstate(ios_base::failbit);
1317 #if __cpp_lib_fstream_native_handle // C++ >= 26
1318 using native_handle_type = typename __filebuf_type::native_handle_type;
1320 [[__gnu__::__always_inline__]]
1322 native_handle() const noexcept
1323 { return _M_filebuf.native_handle(); }
1327 #if __cplusplus >= 201103L
1328 /// Swap specialization for filebufs.
1329 template <class _CharT, class _Traits>
1331 swap(basic_filebuf<_CharT, _Traits>& __x,
1332 basic_filebuf<_CharT, _Traits>& __y)
1335 /// Swap specialization for ifstreams.
1336 template <class _CharT, class _Traits>
1338 swap(basic_ifstream<_CharT, _Traits>& __x,
1339 basic_ifstream<_CharT, _Traits>& __y)
1342 /// Swap specialization for ofstreams.
1343 template <class _CharT, class _Traits>
1345 swap(basic_ofstream<_CharT, _Traits>& __x,
1346 basic_ofstream<_CharT, _Traits>& __y)
1349 /// Swap specialization for fstreams.
1350 template <class _CharT, class _Traits>
1352 swap(basic_fstream<_CharT, _Traits>& __x,
1353 basic_fstream<_CharT, _Traits>& __y)
1357 _GLIBCXX_END_NAMESPACE_VERSION
1360 #include <bits/fstream.tcc>
1362 #endif /* _GLIBCXX_FSTREAM */