[file.streams] (original) (raw)

31.10.1 Header synopsis [fstream.syn]

The header defines four class templates and eight types that associate stream buffers with files and assist reading and writing files.

[Note 1:

The class template basic_filebuf treats a file as a source or sink of bytes.

In an environment that uses a large character set, the file typically holds multibyte character sequences and the basic_filebufobject converts those multibyte sequences into wide character sequences.

— _end note_]

In subclause [file.streams], member functions taking arguments of const filesystem​::​path​::​value_type*are only provided on systems where filesystem​::​path​::​value_type ([fs.class.path]) is not char.

[Note 2:

These functions enable class path support for systems with a wide native path character type, such as wchar_t.

— _end note_]

31.10.2 Native handles [file.native]

Several classes described in [file.streams] have a member native_handle_type.

The type native_handle_type represents a platform-specificnative handle to a file.

It is trivially copyable and models semiregular.

[Note 1:

For operating systems based on POSIX,native_handle_type is int.

For Windows-based operating systems,native_handle_type is HANDLE.

— _end note_]

31.10.3 Class template basic_filebuf [filebuf]

31.10.3.1 General [filebuf.general]

namespace std { template<class charT, class traits = char_traits<charT>> class basic_filebuf : public basic_streambuf<charT, traits> { public: using char_type = charT;using int_type = typename traits::int_type;using pos_type = typename traits::pos_type;using off_type = typename traits::off_type;using traits_type = traits;using native_handle_type = implementation-defined; basic_filebuf(); basic_filebuf(const basic_filebuf&) = delete; basic_filebuf(basic_filebuf&& rhs);virtual ~basic_filebuf(); basic_filebuf& operator=(const basic_filebuf&) = delete; basic_filebuf& operator=(basic_filebuf&& rhs);void swap(basic_filebuf& rhs);bool is_open() const; basic_filebuf* open(const char* s, ios_base::openmode mode); basic_filebuf* open(const filesystem::path::value_type* s, ios_base::openmode mode); basic_filebuf* open(const string& s, ios_base::openmode mode); basic_filebuf* open(const filesystem::path& s, ios_base::openmode mode); basic_filebuf* close(); native_handle_type native_handle() const noexcept;protected: streamsize showmanyc() override; int_type underflow() override; int_type uflow() override; int_type pbackfail(int_type c = traits::eof()) override; int_type overflow (int_type c = traits::eof()) override; basic_streambuf<charT, traits>* setbuf(char_type* s, streamsize n) override; pos_type seekoff(off_type off, ios_base::seekdir way, ios_base::openmode which = ios_base::in | ios_base::out) override; pos_type seekpos(pos_type sp, ios_base::openmode which = ios_base::in | ios_base::out) override;int sync() override;void imbue(const locale& loc) override;};}

The classbasic_filebuf<charT, traits>associates both the input sequence and the output sequence with a file.

The restrictions on reading and writing a sequence controlled by an object of classbasic_filebuf<charT, traits>are the same as for reading and writing with the C standard libraryFILEs.

In particular:

An instance ofbasic_filebufbehaves as described in [filebuf] providedtraits​::​pos_typeisfpos<traits​::​​state_type>.

Otherwise the behavior is undefined.

The file associated with a basic_filebuf has an associated value of type native_handle_type, called the native handle ([file.native]) of that file.

This native handle can be obtained by calling the member function native_handle.

For any opened basic_filebuf f, the native handle returned by f.native_handle() is invalidated when f.close() is called, or f is destroyed.

In order to support file I/O and multibyte/wide character conversion, conversions are performed using members of a facet, referred to asa_codecvt in following subclauses, obtained as if byconst codecvt<charT, char, typename traits::state_type>& a_codecvt = use_facet<codecvt<charT, char, typename traits::state_type>>(getloc());

31.10.3.2 Constructors [filebuf.cons]

Effects: Initializes the base class withbasic_streambuf<charT, traits>() ([streambuf.cons]).

Postconditions: is_open() == false.

basic_filebuf(basic_filebuf&& rhs);

Effects: It is implementation-defined whether the sequence pointers in *this(eback(), gptr(), egptr(),pbase(), pptr(), epptr()) obtain the values which rhs had.

Whether they do or not, *thisand rhs reference separate buffers (if any at all) after the construction.

Additionally *this references the file which rhs did before the construction, andrhs references no file after the construction.

The openmode, locale and any other state of rhs is also copied.

Postconditions: Let rhs_p refer to the state ofrhs just prior to this construction and let rhs_arefer to the state of rhs just after this construction.

virtual ~basic_filebuf();

Effects: Callsclose().

If an exception occurs during the destruction of the object, including the call to close(), the exception is caught but not rethrown (see [res.on.exception.handling]).

31.10.3.3 Assignment and swap [filebuf.assign]

basic_filebuf& operator=(basic_filebuf&& rhs);

Effects: Calls close() then move assigns from rhs.

After the move assignment *this has the observable state it would have had if it had been move constructed from rhs (see [filebuf.cons]).

void swap(basic_filebuf& rhs);

Effects: Exchanges the state of *thisand rhs.

template<class charT, class traits> void swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);

Effects: Equivalent to x.swap(y).

31.10.3.4 Member functions [filebuf.members]

Returns: trueif a previous call toopensucceeded (returned a non-null value) and there has been no intervening call to close.

basic_filebuf* open(const char* s, ios_base::openmode mode); basic_filebuf* open(const filesystem::path::value_type* s, ios_base::openmode mode); // wide systems only; see [[fstream.syn]](#fstream.syn "31.10.1 Header <fstream> synopsis")

Effects: Ifis_open() != false, returns a null pointer.

Otherwise, initializes thefilebufas required.

It then opens the file to which s resolves, if possible, as if by a call to fopen with the second argument determined frommode & ~ios_base​::​ateas indicated in Table 144.

If mode is not some combination of flags shown in the table then the open fails.

Table 144 — File open modes [tab:filebuf.open.modes]

🔗ios_base flag combination stdio equivalent
🔗binary in out trunc app noreplace
🔗 + "w"
🔗 + + "wx"
🔗 + + "w"
🔗 + + + "wx"
🔗 + + "a"
🔗 + "a"
🔗 + "r"
🔗 + + "r+"
🔗 + + + "w+"
🔗 + + + + "w+x"
🔗 + + + "a+"
🔗 + + "a+"
🔗+ + "wb"
🔗+ + + "wbx"
🔗+ + + "wb"
🔗+ + + + "wbx"
🔗+ + + "ab"
🔗+ + "ab"
🔗+ + "rb"
🔗+ + + "r+b"
🔗+ + + + "w+b"
🔗+ + + + + "w+bx"
🔗+ + + + "a+b"
🔗+ + + "a+b"

If the open operation succeeds andios_base​::​ate is set in mode, positions the file to the end (as if by calling fseek(file, 0, SEEK_END), wherefile is the pointer returned by calling fopen).293

If the repositioning operation fails, callsclose()and returns a null pointer to indicate failure.

Returns: thisif successful, a null pointer otherwise.

basic_filebuf* open(const string& s, ios_base::openmode mode); basic_filebuf* open(const filesystem::path& s, ios_base::openmode mode);

Returns: open(s.c_str(), mode);

Effects: Ifis_open() == false, returns a null pointer.

If a put area exists, callsoverflow(traits​::​​eof())to flush characters.

If the last virtual member function called on*this(betweenunderflow,overflow,seekoff, andseekpos) wasoverflowthen callsa_codecvt.unshift(possibly several times) to determine a termination sequence, inserts those characters and callsoverflow(traits​::​​eof())again.

Finally, regardless of whether any of the preceding calls fails or throws an exception, the function closes the file (as if by callingfclose(file)).

If any of the calls made by the function, including fclose, fails,close fails by returning a null pointer.

If one of these calls throws an exception, the exception is caught and rethrown after closing the file.

Postconditions: is_open() == false.

Returns: thison success, a null pointer otherwise.

native_handle_type native_handle() const noexcept;

Preconditions: is_open() is true.

Returns: The native handle associated with *this.

31.10.3.5 Overridden virtual functions [filebuf.virtuals]

streamsize showmanyc() override;

Remarks: An implementation may provide an overriding definition for this function signature if it can determine whether more characters can be read from the input sequence.

int_type underflow() override;

Effects: Behaves according to the description ofbasic_streambuf<charT, traits>​::​underflow(), with the specialization that a sequence of characters is read from the input sequence as if by reading from the associated file into an internal buffer (extern_buf) and then as if by doing:char extern_buf[XSIZE];char* extern_end; charT intern_buf[ISIZE]; charT* intern_end; codecvt_base::result r = a_codecvt.in(state, extern_buf, extern_buf+XSIZE, extern_end, intern_buf, intern_buf+ISIZE, intern_end);

This shall be done in such a way that the class can recover the position (fpos_t) corresponding to each character betweenintern_bufandintern_end.

If the value ofrindicates thata_codecvt.in()ran out of space inintern_buf, retry with a largerintern_buf.

int_type uflow() override;

Effects: Behaves according to the description ofbasic_streambuf<charT, traits>​::​uflow(), with the specialization that a sequence of characters is read from the input with the same method as used byunderflow.

int_type pbackfail(int_type c = traits::eof()) override;

Effects: Puts back the character designated by c to the input sequence, if possible, in one of three ways:

Returns: As specified above, ortraits​::​eof()to indicate failure.

Remarks: Ifis_open() == false, the function always fails.

The function does not put back a character directly to the input sequence.

If the function can succeed in more than one of these ways, it is unspecified which way is chosen.

The function can alter the number of putback positions available as a result of any call.

int_type overflow(int_type c = traits::eof()) override;

Effects: Behaves according to the description ofbasic_streambuf<charT, traits>​::​overflow(c), except that the behavior of “consuming characters” is performed by first converting as if by:charT* b = pbase(); charT* p = pptr(); charT* end;char xbuf[XSIZE];char* xbuf_end; codecvt_base::result r = a_codecvt.out(state, b, p, end, xbuf, xbuf+XSIZE, xbuf_end);and then

Returns: traits​::​not_eof(c)to indicate success, andtraits​::​eof()to indicate failure.

Ifis_open() == false, the function always fails.

basic_streambuf* setbuf(char_type* s, streamsize n) override;

Effects: Ifsetbuf(0, 0)is called on a stream before any I/O has occurred on that stream, the stream becomes unbuffered.

Otherwise the results are implementation-defined.

“Unbuffered” means thatpbase()andpptr()always return null and output to the file should appear as soon as possible.

pos_type seekoff(off_type off, ios_base::seekdir way, ios_base::openmode which= ios_base::in | ios_base::out) override;

Effects: Letwidthdenotea_codecvt.encoding().

Ifis_open() == false, oroff != 0 && width <= 0, then the positioning operation fails.

Otherwise, ifway != basic_ios​::​curoroff != 0, and if the last operation was output, then update the output sequence and write any unshift sequence.

Next, seek to the new position: ifwidth > 0, callfseek(file, width * off, whence), otherwise callfseek(file, 0, whence).

Returns: A newly constructedpos_typeobject that stores the resultant stream position, if possible.

If the positioning operation fails, or if the object cannot represent the resultant stream position, returnspos_type(off_type(-1)).

Remarks: “The last operation was output” means either the last virtual operation was overflow or the put buffer is non-empty.

“Write any unshift sequence” means, ifwidthis less than zero then calla_codecvt.unshift(state, xbuf, xbuf+XSIZE, xbuf_end)and output the resulting unshift sequence.

The function determines one of three values for the argument whence, of typeint, as indicated in Table 145.

pos_type seekpos(pos_type sp, ios_base::openmode which= ios_base::in | ios_base::out) override;

Alters the file position, if possible, to correspond to the position stored in sp (as described below).

Altering the file position performs as follows:

1. if(om & ios_base​::​out) != 0, then update the output sequence and write any unshift sequence;
2. set the file position to sp as if by a call to fsetpos;
3. if(om & ios_base​::​in) != 0, then update the input sequence;

where om is the open mode passed to the last call toopen().

The operation fails ifis_open()returns false.

If sp is an invalid stream position, or if the function positions neither sequence, the positioning operation fails.

If sp has not been obtained by a previous successful call to one of the positioning functions (seekofforseekpos) on the same file the effects are undefined.

Returns: spon success.

Otherwise returnspos_type(off_type(-1)).

Effects: If a put area exists, callsfilebuf​::​overflowto write the characters to the file, then flushes the file as if by calling fflush(file).

If a get area exists, the effect is implementation-defined.

void imbue(const locale& loc) override;

Preconditions: If the file is not positioned at its beginning and the encoding of the current locale as determined bya_codecvt.encoding()is state-dependent ([locale.codecvt.virtuals]) then that facet is the same as the corresponding facet of loc.

Effects: Causes characters inserted or extracted after this call to be converted according to loc until another call ofimbue.

Remarks: This may require reconversion of previously converted characters.

This in turn may require the implementation to be able to reconstruct the original contents of the file.

31.10.4 Class template basic_ifstream [ifstream]

31.10.4.1 General [ifstream.general]

namespace std { template<class charT, class traits = char_traits<charT>> class basic_ifstream : public basic_istream<charT, traits> { public: using char_type = charT;using int_type = typename traits::int_type;using pos_type = typename traits::pos_type;using off_type = typename traits::off_type;using traits_type = traits;using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; basic_ifstream();explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);explicit basic_ifstream(const filesystem::path::value_type* s, ios_base::openmode mode = ios_base::in);explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);template<class T> explicit basic_ifstream(const T& s, ios_base::openmode mode = ios_base::in); basic_ifstream(const basic_ifstream&) = delete; basic_ifstream(basic_ifstream&& rhs); basic_ifstream& operator=(const basic_ifstream&) = delete; basic_ifstream& operator=(basic_ifstream&& rhs);void swap(basic_ifstream& rhs); basic_filebuf<charT, traits>* rdbuf() const; native_handle_type native_handle() const noexcept;bool is_open() const;void open(const char* s, ios_base::openmode mode = ios_base::in);void open(const filesystem::path::value_type* s, ios_base::openmode mode = ios_base::in); void open(const string& s, ios_base::openmode mode = ios_base::in);void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in);void close();private: basic_filebuf<charT, traits> sb; };}

The classbasic_ifstream<charT, traits>supports reading from named files.

It uses abasic_filebuf<​charT, traits>object to control the associated sequence.

For the sake of exposition, the maintained data is presented here as:

31.10.4.2 Constructors [ifstream.cons]

Effects: Initializes the base class withbasic_istream<charT, traits>(addressof(sb)) ([istream.cons]) and sb withbasic_filebuf<charT, traits>() ([filebuf.cons]).

explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);explicit basic_ifstream(const filesystem::path::value_type* s, ios_base::openmode mode = ios_base::in); // wide systems only; see [[fstream.syn]](#fstream.syn "31.10.1 Header <fstream> synopsis")

Effects: Initializes the base class withbasic_istream<charT, traits>(addressof(sb)) ([istream.cons]) and sb withbasic_filebuf<charT, traits>() ([filebuf.cons]), then callsrdbuf()->open(s, mode | ios_base​::​in).

If that function returns a null pointer, callssetstate(failbit).

explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);

Effects: Equivalent to basic_ifstream(s.c_str(), mode).

template<class T> explicit basic_ifstream(const T& s, ios_base::openmode mode = ios_base::in);

Constraints: is_same_v<T, filesystem​::​path> is true.

Effects: Equivalent to basic_ifstream(s.c_str(), mode).

basic_ifstream(basic_ifstream&& rhs);

Effects: Move constructs the base class, and the contained basic_filebuf.

Then calls basic_istream<charT, traits>​::​set_rdbuf(​addressof(sb))to install the contained basic_filebuf.

31.10.4.3 Swap [ifstream.swap]

void swap(basic_ifstream& rhs);

Effects: Exchanges the state of *thisand rhs by callingbasic_istream<charT, traits>​::​swap(rhs) and_sb_.swap(rhs.sb).

template<class charT, class traits> void swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);

Effects: Equivalent to x.swap(y).

31.10.4.4 Member functions [ifstream.members]

basic_filebuf<charT, traits>* rdbuf() const;

Returns: const_cast<basic_filebuf<charT, traits>*>(addressof(sb)).

native_handle_type native_handle() const noexcept;

Effects: Equivalent to: return rdbuf()->native_handle();

Returns: rdbuf()->is_open().

void open(const char* s, ios_base::openmode mode = ios_base::in);void open(const filesystem::path::value_type* s, ios_base::openmode mode = ios_base::in); // wide systems only; see [[fstream.syn]](#fstream.syn "31.10.1 Header <fstream> synopsis")

Effects: Callsrdbuf()->open(s, mode | ios_base​::​in).

If that function does not return a null pointer calls clear(), otherwise callssetstate(failbit)(which may throwios_base​::​failure) ([iostate.flags]).

void open(const string& s, ios_base::openmode mode = ios_base::in);void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in);

Effects: Calls open(s.c_str(), mode).

Effects: Callsrdbuf()->close()and, if that function returns a null pointer, callssetstate(failbit)(which may throwios_base​::​failure) ([iostate.flags]).

31.10.5 Class template basic_ofstream [ofstream]

31.10.5.1 General [ofstream.general]

namespace std { template<class charT, class traits = char_traits<charT>> class basic_ofstream : public basic_ostream<charT, traits> { public: using char_type = charT;using int_type = typename traits::int_type;using pos_type = typename traits::pos_type;using off_type = typename traits::off_type;using traits_type = traits;using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; basic_ofstream();explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);explicit basic_ofstream(const filesystem::path::value_type* s, ios_base::openmode mode = ios_base::out);explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);template<class T> explicit basic_ofstream(const T& s, ios_base::openmode mode = ios_base::out); basic_ofstream(const basic_ofstream&) = delete; basic_ofstream(basic_ofstream&& rhs); basic_ofstream& operator=(const basic_ofstream&) = delete; basic_ofstream& operator=(basic_ofstream&& rhs);void swap(basic_ofstream& rhs); basic_filebuf<charT, traits>* rdbuf() const; native_handle_type native_handle() const noexcept;bool is_open() const;void open(const char* s, ios_base::openmode mode = ios_base::out);void open(const filesystem::path::value_type* s, ios_base::openmode mode = ios_base::out); void open(const string& s, ios_base::openmode mode = ios_base::out);void open(const filesystem::path& s, ios_base::openmode mode = ios_base::out);void close();private: basic_filebuf<charT, traits> sb; };}

The classbasic_ofstream<charT, traits>supports writing to named files.

It uses abasic_filebuf<​charT, traits>object to control the associated sequence.

For the sake of exposition, the maintained data is presented here as:

31.10.5.2 Constructors [ofstream.cons]

Effects: Initializes the base class withbasic_ostream<charT, traits>(addressof(sb)) ([ostream.cons]) and sb withbasic_filebuf<charT, traits>() ([filebuf.cons]).

explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);explicit basic_ofstream(const filesystem::path::value_type* s, ios_base::openmode mode = ios_base::out); // wide systems only; see [[fstream.syn]](#fstream.syn "31.10.1 Header <fstream> synopsis")

Effects: Initializes the base class withbasic_ostream<charT, traits>(addressof(sb)) ([ostream.cons]) and sb withbasic_filebuf<charT, traits>() ([filebuf.cons]), then callsrdbuf()->open(s, mode | ios_base​::​out).

If that function returns a null pointer, callssetstate(​failbit).

explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);

Effects: Equivalent to basic_ofstream(s.c_str(), mode).

template<class T> explicit basic_ofstream(const T& s, ios_base::openmode mode = ios_base::out);

Constraints: is_same_v<T, filesystem​::​path> is true.

Effects: Equivalent to basic_ofstream(s.c_str(), mode).

basic_ofstream(basic_ofstream&& rhs);

Effects: Move constructs the base class, and the contained basic_filebuf.

Then calls basic_ostream<charT, traits>​::​set_rdbuf(​addressof(sb))to install the contained basic_filebuf.

31.10.5.3 Swap [ofstream.swap]

void swap(basic_ofstream& rhs);

Effects: Exchanges the state of *thisand rhs by callingbasic_ostream<charT, traits>​::​swap(rhs) and_sb_.swap(rhs.sb).

template<class charT, class traits> void swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);

Effects: Equivalent to x.swap(y).

31.10.5.4 Member functions [ofstream.members]

basic_filebuf<charT, traits>* rdbuf() const;

Returns: const_cast<basic_filebuf<charT, traits>*>(addressof(sb)).

native_handle_type native_handle() const noexcept;

Effects: Equivalent to: return rdbuf()->native_handle();

Returns: rdbuf()->is_open().

void open(const char* s, ios_base::openmode mode = ios_base::out);void open(const filesystem::path::value_type* s, ios_base::openmode mode = ios_base::out); // wide systems only; see [[fstream.syn]](#fstream.syn "31.10.1 Header <fstream> synopsis")

Effects: Callsrdbuf()->open(s, mode | ios_base​::​out).

If that function does not return a null pointer calls clear(), otherwise callssetstate(​failbit)(which may throwios_base​::​failure) ([iostate.flags]).

Effects: Callsrdbuf()->close()and, if that function fails (returns a null pointer), callssetstate(​failbit)(which may throwios_base​::​failure) ([iostate.flags]).

void open(const string& s, ios_base::openmode mode = ios_base::out);void open(const filesystem::path& s, ios_base::openmode mode = ios_base::out);

Effects: Calls open(s.c_str(), mode).

31.10.6 Class template basic_fstream [fstream]

31.10.6.1 General [fstream.general]

namespace std { template<class charT, class traits = char_traits<charT>> class basic_fstream : public basic_iostream<charT, traits> { public: using char_type = charT;using int_type = typename traits::int_type;using pos_type = typename traits::pos_type;using off_type = typename traits::off_type;using traits_type = traits;using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; basic_fstream();explicit basic_fstream( const char* s, ios_base::openmode mode = ios_base::in | ios_base::out);explicit basic_fstream( const filesystem::path::value_type* s, ios_base::openmode mode = ios_base::in | ios_base::out); explicit basic_fstream( const string& s, ios_base::openmode mode = ios_base::in | ios_base::out);template<class T> explicit basic_fstream(const T& s, ios_base::openmode mode = ios_base::in | ios_base::out); basic_fstream(const basic_fstream&) = delete; basic_fstream(basic_fstream&& rhs); basic_fstream& operator=(const basic_fstream&) = delete; basic_fstream& operator=(basic_fstream&& rhs);void swap(basic_fstream& rhs); basic_filebuf<charT, traits>* rdbuf() const; native_handle_type native_handle() const noexcept;bool is_open() const;void open( const char* s, ios_base::openmode mode = ios_base::in | ios_base::out);void open( const filesystem::path::value_type* s, ios_base::openmode mode = ios_base::in | ios_base::out); void open( const string& s, ios_base::openmode mode = ios_base::in | ios_base::out);void open( const filesystem::path& s, ios_base::openmode mode = ios_base::in | ios_base::out);void close();private: basic_filebuf<charT, traits> sb; };}

The class templatebasic_fstream<charT, traits>supports reading and writing from named files.

It uses abasic_filebuf<charT, traits>object to control the associated sequences.

For the sake of exposition, the maintained data is presented here as:

31.10.6.2 Constructors [fstream.cons]

Effects: Initializes the base class withbasic_iostream<charT, traits>(addressof(sb)) ([iostream.cons]) and_sb_ with basic_filebuf<charT, traits>().

explicit basic_fstream( const char* s, ios_base::openmode mode = ios_base::in | ios_base::out);explicit basic_fstream( const filesystem::path::value_type* s, ios_base::openmode mode = ios_base::in | ios_base::out); // wide systems only; see [[fstream.syn]](#fstream.syn "31.10.1 Header <fstream> synopsis")

Effects: Initializes the base class withbasic_iostream<charT, traits>(addressof(sb)) ([iostream.cons]) and_sb_ with basic_filebuf<charT, traits>().

Then callsrdbuf()->open(s, mode).

If that function returns a null pointer, callssetstate(failbit).

explicit basic_fstream( const string& s, ios_base::openmode mode = ios_base::in | ios_base::out);

Effects: Equivalent to basic_fstream(s.c_str(), mode).

template<class T> explicit basic_fstream(const T& s, ios_base::openmode mode = ios_base::in | ios_base::out);

Constraints: is_same_v<T, filesystem​::​path> is true.

Effects: Equivalent to basic_fstream(s.c_str(), mode).

basic_fstream(basic_fstream&& rhs);

Effects: Move constructs the base class, and the contained basic_filebuf.

Then calls basic_istream<charT, traits>​::​set_rdbuf(​addressof(sb))to install the contained basic_filebuf.

31.10.6.3 Swap [fstream.swap]

void swap(basic_fstream& rhs);

Effects: Exchanges the state of *thisand rhs by callingbasic_iostream<charT,traits>​::​swap(rhs) and_sb_.swap(rhs.sb).

template<class charT, class traits> void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);

Effects: Equivalent to x.swap(y).

31.10.6.4 Member functions [fstream.members]

basic_filebuf<charT, traits>* rdbuf() const;

Returns: const_cast<basic_filebuf<charT, traits>*>(addressof(sb)).

native_handle_type native_handle() const noexcept;

Effects: Equivalent to: return rdbuf()->native_handle();

Returns: rdbuf()->is_open().

void open( const char* s, ios_base::openmode mode = ios_base::in | ios_base::out);void open( const filesystem::path::value_type* s, ios_base::openmode mode = ios_base::in | ios_base::out); // wide systems only; see [[fstream.syn]](#fstream.syn "31.10.1 Header <fstream> synopsis")

Effects: Callsrdbuf()->open(s, mode).

If that function does not return a null pointer calls clear(), otherwise callssetstate(failbit)(which may throwios_base​::​failure) ([iostate.flags]).

void open( const string& s, ios_base::openmode mode = ios_base::in | ios_base::out);void open( const filesystem::path& s, ios_base::openmode mode = ios_base::in | ios_base::out);

Effects: Calls open(s.c_str(), mode).

Effects: Callsrdbuf()->close()and, if that function returns a null pointer, callssetstate(failbit)(which may throwios_base​::​failure) ([iostate.flags]).