1// File based streams -*- C++ -*-
3// Copyright (C) 1997-2025 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
37#pragma GCC system_header
40#include <bits/requires_hosted.h> // iostreams
44#include <bits/codecvt.h>
45#include <cstdio> // For BUFSIZ
46#include <bits/basic_file.h> // For __basic_file, __c_lock
47#if __cplusplus >= 201103L
48#include <string> // For std::string overloads.
51#define __glibcxx_want_fstream_native_handle
52#include <bits/version.h>
54// This can be overridden by the target's os_defines.h
55#ifndef _GLIBCXX_BUFSIZ
56# define _GLIBCXX_BUFSIZ BUFSIZ
59namespace std _GLIBCXX_VISIBILITY(default)
61_GLIBCXX_BEGIN_NAMESPACE_VERSION
63#if __cplusplus >= 201703L
64 // Enable if _Path is a filesystem::path or experimental::filesystem::path
65 template<typename _Path, typename _Result = _Path, typename _Path2
66 = decltype(std::declval<_Path&>().make_preferred().filename())>
67 using _If_fs_path = enable_if_t<is_same_v<_Path, _Path2>, _Result>;
71 // [27.8.1.1] template class basic_filebuf
73 * @brief The actual work of input and output (for files).
76 * @tparam _CharT Type of character stream.
77 * @tparam _Traits Traits for character type, defaults to
78 * char_traits<_CharT>.
80 * This class associates both its input and output sequence with an
81 * external disk file, and maintains a joint file position for both
82 * sequences. Many of its semantics are described in terms of similar
83 * behavior in the Standard C Library's @c FILE streams.
85 * Requirements on traits_type, specific to this class:
86 * - traits_type::pos_type must be fpos<traits_type::state_type>
87 * - traits_type::off_type must be streamoff
88 * - traits_type::state_type must be Assignable and DefaultConstructible,
89 * - traits_type::state_type() must be the initial state for codecvt.
91 template<typename _CharT, typename _Traits>
92 class basic_filebuf : public basic_streambuf<_CharT, _Traits>
94#if __cplusplus >= 201103L
95 template<typename _Tp>
96 using __chk_state = __and_<is_copy_assignable<_Tp>,
97 is_copy_constructible<_Tp>,
98 is_default_constructible<_Tp>>;
100 static_assert(__chk_state<typename _Traits::state_type>::value,
101 "state_type must be CopyAssignable, CopyConstructible"
102 " and DefaultConstructible");
104 static_assert(is_same<typename _Traits::pos_type,
105 fpos<typename _Traits::state_type>>::value,
106 "pos_type must be fpos<state_type>");
110 typedef _CharT char_type;
111 typedef _Traits traits_type;
112 typedef typename traits_type::int_type int_type;
113 typedef typename traits_type::pos_type pos_type;
114 typedef typename traits_type::off_type off_type;
116 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
117 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
118 typedef __basic_file<char> __file_type;
119 typedef typename traits_type::state_type __state_type;
120 typedef codecvt<char_type, char, __state_type> __codecvt_type;
122 friend class ios_base; // For sync_with_stdio.
126 // MT lock inherited from libio or other low-level io library.
132 /// Place to stash in || out || in | out settings for current filebuf.
133 ios_base::openmode _M_mode;
135 // Beginning state type for codecvt.
136 __state_type _M_state_beg;
138 // During output, the state that corresponds to pptr(),
139 // during input, the state that corresponds to egptr() and
141 __state_type _M_state_cur;
143 // Not used for output. During input, the state that corresponds
144 // to eback() and _M_ext_buf.
145 __state_type _M_state_last;
147 /// Pointer to the beginning of internal buffer.
151 * Actual size of internal buffer. This number is equal to the size
152 * of the put area + 1 position, reserved for the overflow char of
157 // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
158 bool _M_buf_allocated;
161 * _M_reading == false && _M_writing == false for @b uncommitted mode;
162 * _M_reading == true for @b read mode;
163 * _M_writing == true for @b write mode;
165 * NB: _M_reading == true && _M_writing == true is unused.
172 * Necessary bits for putback buffer management.
174 * @note pbacks of over one character are not currently supported.
177 char_type* _M_pback_cur_save;
178 char_type* _M_pback_end_save;
182 // Cached codecvt facet.
183 const __codecvt_type* _M_codecvt;
186 * Buffer for external characters. Used for input when
187 * codecvt::always_noconv() == false. When valid, this corresponds
193 * Size of buffer held by _M_ext_buf.
195 streamsize _M_ext_buf_size;
198 * Pointers into the buffer held by _M_ext_buf that delimit a
199 * subsequence of bytes that have been read but not yet converted.
200 * When valid, _M_ext_next corresponds to egptr().
202 const char* _M_ext_next;
206 * Initializes pback buffers, and moves normal buffers to safety.
208 * _M_in_cur has already been moved back
215 _M_pback_cur_save = this->gptr();
216 _M_pback_end_save = this->egptr();
217 this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
218 _M_pback_init = true;
223 * Deactivates pback buffer contents, and restores normal buffer.
225 * The pback buffer has only moved forward.
228 _M_destroy_pback() throw()
232 // Length _M_in_cur moved in the pback buffer.
233 _M_pback_cur_save += this->gptr() != this->eback();
234 this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
235 _M_pback_init = false;
240 // Constructors/destructor:
242 * @brief Does not open any files.
244 * The default constructor initializes the parent class using its
249#if __cplusplus >= 201103L
250 basic_filebuf(const basic_filebuf&) = delete;
251 basic_filebuf(basic_filebuf&&);
255 * @brief The destructor closes the file first.
266#if __cplusplus >= 201103L
267 basic_filebuf& operator=(const basic_filebuf&) = delete;
268 basic_filebuf& operator=(basic_filebuf&&);
269 void swap(basic_filebuf&);
274 * @brief Returns true if the external file is open.
278 is_open() const throw()
279 { return _M_file.is_open(); }
282 * @brief Opens an external file.
283 * @param __s The name of the file.
284 * @param __mode The open mode flags.
285 * @return @c this on success, NULL on failure
287 * If a file is already open, this function immediately fails.
288 * Otherwise it tries to open the file named @a __s using the flags
289 * given in @a __mode.
291 * Table 92, adapted here, gives the relation between openmode
292 * combinations and the equivalent @c fopen() flags.
293 * (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,
294 * and binary|in|app per DR 596)
296 * +---------------------------------------------------------+
297 * | ios_base Flag combination stdio equivalent |
298 * |binary in out trunc app |
299 * +---------------------------------------------------------+
309 * +---------------------------------------------------------+
319 * +---------------------------------------------------------+
323 open(const char* __s, ios_base::openmode __mode);
325#if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
327 * @brief Opens an external file.
328 * @param __s The name of the file, as a wide character string.
329 * @param __mode The open mode flags.
330 * @return @c this on success, NULL on failure
333 open(const wchar_t* __s, ios_base::openmode __mode);
336#if __cplusplus >= 201103L
338 * @brief Opens an external file.
339 * @param __s The name of the file.
340 * @param __mode The open mode flags.
341 * @return @c this on success, NULL on failure
344 open(const std::string& __s, ios_base::openmode __mode)
345 { return open(__s.c_str(), __mode); }
347#if __cplusplus >= 201703L
349 * @brief Opens an external file.
350 * @param __s The name of the file, as a filesystem::path.
351 * @param __mode The open mode flags.
352 * @return @c this on success, NULL on failure
354 template<typename _Path>
355 _If_fs_path<_Path, __filebuf_type*>
356 open(const _Path& __s, ios_base::openmode __mode)
357 { return open(__s.c_str(), __mode); }
362 * @brief Closes the currently associated file.
363 * @return @c this on success, NULL on failure
365 * If no file is currently open, this function immediately fails.
367 * If a <em>put buffer area</em> exists, @c overflow(eof) is
368 * called to flush all the characters. The file is then
371 * If any operations fail, this function also fails.
376#if __cpp_lib_fstream_native_handle // C++ >= 26
378 * @brief The platform-specific file handle type.
380 * The type is `int` for POSIX platforms that use file descriptors,
381 * or `HANDLE` for Windows, or `FILE*` if the library was configured
382 * with `--enable-cstdio=stdio_pure`.
386 using native_handle_type = typename __file_type::native_handle_type;
389 * @brief Return the platform-specific native handle for the file.
390 * @pre `is_open()` is true.
391 * @return The native file handle associated with `*this`.
393 * The handle is invalidated when this filebuf is closed or destroyed.
397 [[__gnu__::__always_inline__]]
399 native_handle() const noexcept
401 __glibcxx_assert(is_open());
402 return _M_file.native_handle();
408 _M_allocate_internal_buffer();
411 _M_destroy_internal_buffer() throw();
413 // [27.8.1.4] overridden virtual functions
417 // Stroustrup, 1998, p. 628
418 // underflow() and uflow() functions are called to get the next
419 // character from the real input source when the buffer is empty.
420 // Buffered input uses underflow()
426 pbackfail(int_type __c = _Traits::eof());
428 // Stroustrup, 1998, p 648
429 // The overflow() function is called to transfer characters to the
430 // real output destination when the buffer is full. A call to
431 // overflow(c) outputs the contents of the buffer plus the
434 // Consume some sequence of the characters in the pending sequence.
436 overflow(int_type __c = _Traits::eof());
438 // Convert internal byte sequence to external, char-based
439 // sequence via codecvt.
441 _M_convert_to_external(char_type*, streamsize);
444 * @brief Manipulates the buffer.
445 * @param __s Pointer to a buffer area.
446 * @param __n Size of @a __s.
449 * If no file has been opened, and both @a __s and @a __n are zero, then
450 * the stream becomes unbuffered. Otherwise, @c __s is used as a
452 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
455 virtual __streambuf_type*
456 setbuf(char_type* __s, streamsize __n);
459 seekoff(off_type __off, ios_base::seekdir __way,
460 ios_base::openmode __mode = ios_base::in | ios_base::out);
463 seekpos(pos_type __pos,
464 ios_base::openmode __mode = ios_base::in | ios_base::out);
466 // Common code for seekoff, seekpos, and overflow
468 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
471 _M_get_ext_pos(__state_type &__state);
477 imbue(const locale& __loc);
480 xsgetn(char_type* __s, streamsize __n);
483 xsputn(const char_type* __s, streamsize __n);
485 // Flushes output buffer, then writes unshift sequence.
487 _M_terminate_output();
490 * This function sets the pointers of the internal buffer, both get
491 * and put areas. Typically:
493 * __off == egptr() - eback() upon underflow/uflow (@b read mode);
494 * __off == 0 upon overflow (@b write mode);
495 * __off == -1 upon open, setbuf, seekoff/pos (@b uncommitted mode).
497 * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
498 * reflects the actual allocated memory and the last cell is reserved
499 * for the overflow char of a full put area.
502 _M_set_buffer(streamsize __off)
504 const bool __testin = _M_mode & ios_base::in;
505 const bool __testout = (_M_mode & ios_base::out
506 || _M_mode & ios_base::app);
508 if (__testin && __off > 0)
509 this->setg(_M_buf, _M_buf, _M_buf + __off);
511 this->setg(_M_buf, _M_buf, _M_buf);
513 if (__testout && __off == 0 && _M_buf_size > 1 )
514 this->setp(_M_buf, _M_buf + _M_buf_size - 1);
520 // [27.8.1.5] Template class basic_ifstream
522 * @brief Controlling input for files.
525 * @tparam _CharT Type of character stream.
526 * @tparam _Traits Traits for character type, defaults to
527 * char_traits<_CharT>.
529 * This class supports reading from named files, using the inherited
530 * functions from std::basic_istream. To control the associated
531 * sequence, an instance of std::basic_filebuf is used, which this page
532 * refers to as @c sb.
534 template<typename _CharT, typename _Traits>
535 class basic_ifstream : public basic_istream<_CharT, _Traits>
539 typedef _CharT char_type;
540 typedef _Traits traits_type;
541 typedef typename traits_type::int_type int_type;
542 typedef typename traits_type::pos_type pos_type;
543 typedef typename traits_type::off_type off_type;
545 // Non-standard types:
546 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
547 typedef basic_istream<char_type, traits_type> __istream_type;
550 __filebuf_type _M_filebuf;
553 // Constructors/Destructors:
555 * @brief Default constructor.
557 * Initializes @c sb using its default constructor, and passes
558 * @c &sb to the base class initializer. Does not open any files
559 * (you haven't given it a filename to open).
561 basic_ifstream() : __istream_type(), _M_filebuf()
562 { this->init(&_M_filebuf); }
565 * @brief Create an input file stream.
566 * @param __s Null terminated string specifying the filename.
567 * @param __mode Open file in specified mode (see std::ios_base).
569 * @c ios_base::in is automatically included in @a __mode.
572 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
573 : __istream_type(), _M_filebuf()
575 this->init(&_M_filebuf);
576 this->open(__s, __mode);
579#if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
581 * @param Create an input file stream.
582 * @param __s Wide string specifying the filename.
583 * @param __mode Open file in specified mode (see std::ios_base).
585 * @c ios_base::in is automatically included in @a __mode.
587 basic_ifstream(const wchar_t* __s,
588 ios_base::openmode __mode = ios_base::in)
589 : __istream_type(), _M_filebuf()
591 this->init(&_M_filebuf);
592 this->open(__s, __mode);
596#if __cplusplus >= 201103L
598 * @brief Create an input file stream.
599 * @param __s std::string specifying the filename.
600 * @param __mode Open file in specified mode (see std::ios_base).
602 * @c ios_base::in is automatically included in @a __mode.
605 basic_ifstream(const std::string& __s,
606 ios_base::openmode __mode = ios_base::in)
607 : __istream_type(), _M_filebuf()
609 this->init(&_M_filebuf);
610 this->open(__s, __mode);
613#if __cplusplus >= 201703L
615 * @brief Create an input file stream.
616 * @param __s filesystem::path specifying the filename.
617 * @param __mode Open file in specified mode (see std::ios_base).
619 * @c ios_base::in is automatically included in @a __mode.
621 template<typename _Path, typename _Require = _If_fs_path<_Path>>
622 basic_ifstream(const _Path& __s,
623 ios_base::openmode __mode = ios_base::in)
624 : basic_ifstream(__s.c_str(), __mode)
628 basic_ifstream(const basic_ifstream&) = delete;
630 basic_ifstream(basic_ifstream&& __rhs)
631 : __istream_type(std::move(__rhs)),
632 _M_filebuf(std::move(__rhs._M_filebuf))
633 { __istream_type::set_rdbuf(&_M_filebuf); }
637 * @brief The destructor does nothing.
639 * The file is closed by the filebuf object, not the formatting
645#if __cplusplus >= 201103L
646 // 27.8.3.2 Assign and swap:
649 operator=(const basic_ifstream&) = delete;
652 operator=(basic_ifstream&& __rhs)
654 __istream_type::operator=(std::move(__rhs));
655 _M_filebuf = std::move(__rhs._M_filebuf);
660 swap(basic_ifstream& __rhs)
662 __istream_type::swap(__rhs);
663 _M_filebuf.swap(__rhs._M_filebuf);
669 * @brief Accessing the underlying buffer.
670 * @return The current basic_filebuf buffer.
672 * This hides both signatures of std::basic_ios::rdbuf().
677 { return const_cast<__filebuf_type*>(&_M_filebuf); }
680 * @brief Wrapper to test for an open file.
681 * @return @c rdbuf()->is_open()
686 { return _M_filebuf.is_open(); }
688 // _GLIBCXX_RESOLVE_LIB_DEFECTS
689 // 365. Lack of const-qualification in clause 27
693 { return _M_filebuf.is_open(); }
696 * @brief Opens an external file.
697 * @param __s The name of the file.
698 * @param __mode The open mode flags.
700 * Calls @c std::basic_filebuf::open(s,__mode|in). If that function
701 * fails, @c failbit is set in the stream's error state.
704 open(const char* __s, ios_base::openmode __mode = ios_base::in)
706 if (!_M_filebuf.open(__s, __mode | ios_base::in))
707 this->setstate(ios_base::failbit);
709 // _GLIBCXX_RESOLVE_LIB_DEFECTS
710 // 409. Closing an fstream should clear error state
714#if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
716 * @brief Opens an external file.
717 * @param __s The name of the file, as a wide character string.
718 * @param __mode The open mode flags.
720 * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
721 * fails, @c failbit is set in the stream's error state.
724 open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in)
726 if (!_M_filebuf.open(__s, __mode | ios_base::in))
727 this->setstate(ios_base::failbit);
733#if __cplusplus >= 201103L
735 * @brief Opens an external file.
736 * @param __s The name of the file.
737 * @param __mode The open mode flags.
739 * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
740 * fails, @c failbit is set in the stream's error state.
743 open(const std::string& __s, ios_base::openmode __mode = ios_base::in)
745 if (!_M_filebuf.open(__s, __mode | ios_base::in))
746 this->setstate(ios_base::failbit);
748 // _GLIBCXX_RESOLVE_LIB_DEFECTS
749 // 409. Closing an fstream should clear error state
753#if __cplusplus >= 201703L
755 * @brief Opens an external file.
756 * @param __s The name of the file, as a filesystem::path.
757 * @param __mode The open mode flags.
759 * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
760 * fails, @c failbit is set in the stream's error state.
762 template<typename _Path>
763 _If_fs_path<_Path, void>
764 open(const _Path& __s, ios_base::openmode __mode = ios_base::in)
765 { open(__s.c_str(), __mode); }
770 * @brief Close the file.
772 * Calls @c std::basic_filebuf::close(). If that function
773 * fails, @c failbit is set in the stream's error state.
778 if (!_M_filebuf.close())
779 this->setstate(ios_base::failbit);
782#if __cpp_lib_fstream_native_handle // C++ >= 26
783 using native_handle_type = typename __filebuf_type::native_handle_type;
785 [[__gnu__::__always_inline__]]
787 native_handle() const noexcept
788 { return _M_filebuf.native_handle(); }
793 // [27.8.1.8] Template class basic_ofstream
795 * @brief Controlling output for files.
798 * @tparam _CharT Type of character stream.
799 * @tparam _Traits Traits for character type, defaults to
800 * char_traits<_CharT>.
802 * This class supports reading from named files, using the inherited
803 * functions from std::basic_ostream. To control the associated
804 * sequence, an instance of std::basic_filebuf is used, which this page
805 * refers to as @c sb.
807 template<typename _CharT, typename _Traits>
808 class basic_ofstream : public basic_ostream<_CharT,_Traits>
812 typedef _CharT char_type;
813 typedef _Traits traits_type;
814 typedef typename traits_type::int_type int_type;
815 typedef typename traits_type::pos_type pos_type;
816 typedef typename traits_type::off_type off_type;
818 // Non-standard types:
819 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
820 typedef basic_ostream<char_type, traits_type> __ostream_type;
823 __filebuf_type _M_filebuf;
828 * @brief Default constructor.
830 * Initializes @c sb using its default constructor, and passes
831 * @c &sb to the base class initializer. Does not open any files
832 * (you haven't given it a filename to open).
834 basic_ofstream(): __ostream_type(), _M_filebuf()
835 { this->init(&_M_filebuf); }
838 * @brief Create an output file stream.
839 * @param __s Null terminated string specifying the filename.
840 * @param __mode Open file in specified mode (see std::ios_base).
842 * @c ios_base::out is automatically included in @a __mode.
845 basic_ofstream(const char* __s,
846 ios_base::openmode __mode = ios_base::out)
847 : __ostream_type(), _M_filebuf()
849 this->init(&_M_filebuf);
850 this->open(__s, __mode);
853#if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
855 * @param Create an output file stream.
856 * @param __s Wide string specifying the filename.
857 * @param __mode Open file in specified mode (see std::ios_base).
859 * @c ios_base::out | @c ios_base::trunc is automatically included in
862 basic_ofstream(const wchar_t* __s,
863 ios_base::openmode __mode = ios_base::out|ios_base::trunc)
864 : __ostream_type(), _M_filebuf()
866 this->init(&_M_filebuf);
867 this->open(__s, __mode);
871#if __cplusplus >= 201103L
873 * @brief Create an output file stream.
874 * @param __s std::string specifying the filename.
875 * @param __mode Open file in specified mode (see std::ios_base).
877 * @c ios_base::out is automatically included in @a __mode.
880 basic_ofstream(const std::string& __s,
881 ios_base::openmode __mode = ios_base::out)
882 : __ostream_type(), _M_filebuf()
884 this->init(&_M_filebuf);
885 this->open(__s, __mode);
888#if __cplusplus >= 201703L
890 * @brief Create an output file stream.
891 * @param __s filesystem::path specifying the filename.
892 * @param __mode Open file in specified mode (see std::ios_base).
894 * @c ios_base::out is automatically included in @a __mode.
896 template<typename _Path, typename _Require = _If_fs_path<_Path>>
897 basic_ofstream(const _Path& __s,
898 ios_base::openmode __mode = ios_base::out)
899 : basic_ofstream(__s.c_str(), __mode)
903 basic_ofstream(const basic_ofstream&) = delete;
905 basic_ofstream(basic_ofstream&& __rhs)
906 : __ostream_type(std::move(__rhs)),
907 _M_filebuf(std::move(__rhs._M_filebuf))
908 { __ostream_type::set_rdbuf(&_M_filebuf); }
912 * @brief The destructor does nothing.
914 * The file is closed by the filebuf object, not the formatting
920#if __cplusplus >= 201103L
921 // 27.8.3.2 Assign and swap:
924 operator=(const basic_ofstream&) = delete;
927 operator=(basic_ofstream&& __rhs)
929 __ostream_type::operator=(std::move(__rhs));
930 _M_filebuf = std::move(__rhs._M_filebuf);
935 swap(basic_ofstream& __rhs)
937 __ostream_type::swap(__rhs);
938 _M_filebuf.swap(__rhs._M_filebuf);
944 * @brief Accessing the underlying buffer.
945 * @return The current basic_filebuf buffer.
947 * This hides both signatures of std::basic_ios::rdbuf().
952 { return const_cast<__filebuf_type*>(&_M_filebuf); }
955 * @brief Wrapper to test for an open file.
956 * @return @c rdbuf()->is_open()
961 { return _M_filebuf.is_open(); }
963 // _GLIBCXX_RESOLVE_LIB_DEFECTS
964 // 365. Lack of const-qualification in clause 27
968 { return _M_filebuf.is_open(); }
971 * @brief Opens an external file.
972 * @param __s The name of the file.
973 * @param __mode The open mode flags.
975 * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
976 * function fails, @c failbit is set in the stream's error state.
979 open(const char* __s, ios_base::openmode __mode = ios_base::out)
981 if (!_M_filebuf.open(__s, __mode | ios_base::out))
982 this->setstate(ios_base::failbit);
984 // _GLIBCXX_RESOLVE_LIB_DEFECTS
985 // 409. Closing an fstream should clear error state
989#if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
991 * @brief Opens an external file.
992 * @param __s The name of the file.
993 * @param __mode The open mode flags.
995 * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
996 * function fails, @c failbit is set in the stream's error state.
999 open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out)
1001 if (!_M_filebuf.open(__s, __mode | ios_base::out))
1002 this->setstate(ios_base::failbit);
1008#if __cplusplus >= 201103L
1010 * @brief Opens an external file.
1011 * @param __s The name of the file.
1012 * @param __mode The open mode flags.
1014 * Calls @c std::basic_filebuf::open(s,mode|out). If that
1015 * function fails, @c failbit is set in the stream's error state.
1018 open(const std::string& __s, ios_base::openmode __mode = ios_base::out)
1020 if (!_M_filebuf.open(__s, __mode | ios_base::out))
1021 this->setstate(ios_base::failbit);
1023 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1024 // 409. Closing an fstream should clear error state
1028#if __cplusplus >= 201703L
1030 * @brief Opens an external file.
1031 * @param __s The name of the file, as a filesystem::path.
1032 * @param __mode The open mode flags.
1034 * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
1035 * function fails, @c failbit is set in the stream's error state.
1037 template<typename _Path>
1038 _If_fs_path<_Path, void>
1039 open(const _Path& __s, ios_base::openmode __mode = ios_base::out)
1040 { open(__s.c_str(), __mode); }
1045 * @brief Close the file.
1047 * Calls @c std::basic_filebuf::close(). If that function
1048 * fails, @c failbit is set in the stream's error state.
1053 if (!_M_filebuf.close())
1054 this->setstate(ios_base::failbit);
1057#if __cpp_lib_fstream_native_handle // C++ >= 26
1058 using native_handle_type = typename __filebuf_type::native_handle_type;
1060 [[__gnu__::__always_inline__]]
1062 native_handle() const noexcept
1063 { return _M_filebuf.native_handle(); }
1068 // [27.8.1.11] Template class basic_fstream
1070 * @brief Controlling input and output for files.
1073 * @tparam _CharT Type of character stream.
1074 * @tparam _Traits Traits for character type, defaults to
1075 * char_traits<_CharT>.
1077 * This class supports reading from and writing to named files, using
1078 * the inherited functions from std::basic_iostream. To control the
1079 * associated sequence, an instance of std::basic_filebuf is used, which
1080 * this page refers to as @c sb.
1082 template<typename _CharT, typename _Traits>
1083 class basic_fstream : public basic_iostream<_CharT, _Traits>
1087 typedef _CharT char_type;
1088 typedef _Traits traits_type;
1089 typedef typename traits_type::int_type int_type;
1090 typedef typename traits_type::pos_type pos_type;
1091 typedef typename traits_type::off_type off_type;
1093 // Non-standard types:
1094 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
1095 typedef basic_ios<char_type, traits_type> __ios_type;
1096 typedef basic_iostream<char_type, traits_type> __iostream_type;
1099 __filebuf_type _M_filebuf;
1102 // Constructors/destructor:
1104 * @brief Default constructor.
1106 * Initializes @c sb using its default constructor, and passes
1107 * @c &sb to the base class initializer. Does not open any files
1108 * (you haven't given it a filename to open).
1111 : __iostream_type(), _M_filebuf()
1112 { this->init(&_M_filebuf); }
1115 * @brief Create an input/output file stream.
1116 * @param __s Null terminated string specifying the filename.
1117 * @param __mode Open file in specified mode (see std::ios_base).
1120 basic_fstream(const char* __s,
1121 ios_base::openmode __mode = ios_base::in | ios_base::out)
1122 : __iostream_type(0), _M_filebuf()
1124 this->init(&_M_filebuf);
1125 this->open(__s, __mode);
1128#if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
1130 * @param Create an input/output file stream.
1131 * @param __s Wide string specifying the filename.
1132 * @param __mode Open file in specified mode (see std::ios_base).
1134 basic_fstream(const wchar_t* __s,
1135 ios_base::openmode __mode = ios_base::in | ios_base::out)
1136 : __iostream_type(0), _M_filebuf()
1138 this->init(&_M_filebuf);
1139 this->open(__s, __mode);
1143#if __cplusplus >= 201103L
1145 * @brief Create an input/output file stream.
1146 * @param __s Null terminated string specifying the filename.
1147 * @param __mode Open file in specified mode (see std::ios_base).
1150 basic_fstream(const std::string& __s,
1151 ios_base::openmode __mode = ios_base::in | ios_base::out)
1152 : __iostream_type(0), _M_filebuf()
1154 this->init(&_M_filebuf);
1155 this->open(__s, __mode);
1158#if __cplusplus >= 201703L
1160 * @brief Create an input/output file stream.
1161 * @param __s filesystem::path specifying the filename.
1162 * @param __mode Open file in specified mode (see std::ios_base).
1164 template<typename _Path, typename _Require = _If_fs_path<_Path>>
1165 basic_fstream(const _Path& __s,
1166 ios_base::openmode __mode = ios_base::in | ios_base::out)
1167 : basic_fstream(__s.c_str(), __mode)
1171 basic_fstream(const basic_fstream&) = delete;
1173 basic_fstream(basic_fstream&& __rhs)
1174 : __iostream_type(std::move(__rhs)),
1175 _M_filebuf(std::move(__rhs._M_filebuf))
1176 { __iostream_type::set_rdbuf(&_M_filebuf); }
1180 * @brief The destructor does nothing.
1182 * The file is closed by the filebuf object, not the formatting
1188#if __cplusplus >= 201103L
1189 // 27.8.3.2 Assign and swap:
1192 operator=(const basic_fstream&) = delete;
1195 operator=(basic_fstream&& __rhs)
1197 __iostream_type::operator=(std::move(__rhs));
1198 _M_filebuf = std::move(__rhs._M_filebuf);
1203 swap(basic_fstream& __rhs)
1205 __iostream_type::swap(__rhs);
1206 _M_filebuf.swap(__rhs._M_filebuf);
1212 * @brief Accessing the underlying buffer.
1213 * @return The current basic_filebuf buffer.
1215 * This hides both signatures of std::basic_ios::rdbuf().
1220 { return const_cast<__filebuf_type*>(&_M_filebuf); }
1223 * @brief Wrapper to test for an open file.
1224 * @return @c rdbuf()->is_open()
1229 { return _M_filebuf.is_open(); }
1231 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1232 // 365. Lack of const-qualification in clause 27
1236 { return _M_filebuf.is_open(); }
1239 * @brief Opens an external file.
1240 * @param __s The name of the file.
1241 * @param __mode The open mode flags.
1243 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1244 * function fails, @c failbit is set in the stream's error state.
1247 open(const char* __s,
1248 ios_base::openmode __mode = ios_base::in | ios_base::out)
1250 if (!_M_filebuf.open(__s, __mode))
1251 this->setstate(ios_base::failbit);
1253 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1254 // 409. Closing an fstream should clear error state
1258#if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
1260 * @brief Opens an external file.
1261 * @param __s The name of the file.
1262 * @param __mode The open mode flags.
1264 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1265 * function fails, @c failbit is set in the stream's error state.
1268 open(const wchar_t* __s,
1269 ios_base::openmode __mode = ios_base::in | ios_base::out)
1271 if (!_M_filebuf.open(__s, __mode))
1272 this->setstate(ios_base::failbit);
1278#if __cplusplus >= 201103L
1280 * @brief Opens an external file.
1281 * @param __s The name of the file.
1282 * @param __mode The open mode flags.
1284 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1285 * function fails, @c failbit is set in the stream's error state.
1288 open(const std::string& __s,
1289 ios_base::openmode __mode = ios_base::in | ios_base::out)
1291 if (!_M_filebuf.open(__s, __mode))
1292 this->setstate(ios_base::failbit);
1294 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1295 // 409. Closing an fstream should clear error state
1299#if __cplusplus >= 201703L
1301 * @brief Opens an external file.
1302 * @param __s The name of the file, as a filesystem::path.
1303 * @param __mode The open mode flags.
1305 * Calls @c std::basic_filebuf::open(__s,__mode). If that
1306 * function fails, @c failbit is set in the stream's error state.
1308 template<typename _Path>
1309 _If_fs_path<_Path, void>
1310 open(const _Path& __s,
1311 ios_base::openmode __mode = ios_base::in | ios_base::out)
1312 { open(__s.c_str(), __mode); }
1317 * @brief Close the file.
1319 * Calls @c std::basic_filebuf::close(). If that function
1320 * fails, @c failbit is set in the stream's error state.
1325 if (!_M_filebuf.close())
1326 this->setstate(ios_base::failbit);
1329#if __cpp_lib_fstream_native_handle // C++ >= 26
1330 using native_handle_type = typename __filebuf_type::native_handle_type;
1332 [[__gnu__::__always_inline__]]
1334 native_handle() const noexcept
1335 { return _M_filebuf.native_handle(); }
1339#if __cplusplus >= 201103L
1340 /// Swap specialization for filebufs.
1341 template <class _CharT, class _Traits>
1343 swap(basic_filebuf<_CharT, _Traits>& __x,
1344 basic_filebuf<_CharT, _Traits>& __y)
1347 /// Swap specialization for ifstreams.
1348 template <class _CharT, class _Traits>
1350 swap(basic_ifstream<_CharT, _Traits>& __x,
1351 basic_ifstream<_CharT, _Traits>& __y)
1354 /// Swap specialization for ofstreams.
1355 template <class _CharT, class _Traits>
1357 swap(basic_ofstream<_CharT, _Traits>& __x,
1358 basic_ofstream<_CharT, _Traits>& __y)
1361 /// Swap specialization for fstreams.
1362 template <class _CharT, class _Traits>
1364 swap(basic_fstream<_CharT, _Traits>& __x,
1365 basic_fstream<_CharT, _Traits>& __y)
1369_GLIBCXX_END_NAMESPACE_VERSION
1372#include <bits/fstream.tcc>
1374#endif /* _GLIBCXX_FSTREAM */