[ifstream]

# 31 Input/output library [[input.output]](./#input.output)

## 31.10 File-based streams [[file.streams]](file.streams#ifstream)

### 31.10.4 Class template basic_ifstream [ifstream]

#### [31.10.4.1](#general) General [[ifstream.general]](ifstream.general)

[ð](#lib:basic_ifstream)

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; // [[ifstream.cons]](#cons "31.10.4.2 Constructors"), constructors 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);// wide systems only; see [[fstream.syn]](fstream.syn "31.10.1 Header <fstream> synopsis")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); // [[ifstream.swap]](#swap "31.10.4.3 Swap"), swapvoid swap(basic_ifstream& rhs); // [[ifstream.members]](#members "31.10.4.4 Member functions"), members 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); // wide systems only; see [[fstream.syn]](fstream.syn "31.10.1 Header <fstream> synopsis")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*; // *exposition only*};}

[1](#general-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11821)

The classbasic_ifstream<charT, traits> supports reading from named files[.](#general-1.sentence-1)

It uses abasic_filebuf<​charT, traits> object to control the associated
sequence[.](#general-1.sentence-2)

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

- [(1.1)](#general-1.1)

*sb*, the filebuf object[.](#general-1.sentence-3)

#### [31.10.4.2](#cons) Constructors [[ifstream.cons]](ifstream.cons)

[ð](#lib:basic_ifstream,constructor)

`basic_ifstream();
`

[1](#cons-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11843)

*Effects*: Initializes the base class withbasic_istream<charT, traits>(addressof(*sb*)) ([[istream.cons]](istream.cons "31.7.5.2.2 Constructors"))
and *sb* withbasic_filebuf<charT, traits>() ([[filebuf.cons]](filebuf.cons "31.10.3.2 Constructors"))[.](#cons-1.sentence-1)

[ð](#lib:basic_ifstream,constructor_)

`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")
`

[2](#cons-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11860)

*Effects*: Initializes the base class withbasic_istream<charT, traits>(addressof(*sb*)) ([[istream.cons]](istream.cons "31.7.5.2.2 Constructors"))
and *sb* withbasic_filebuf<charT, traits>() ([[filebuf.cons]](filebuf.cons "31.10.3.2 Constructors")),
then callsrdbuf()->open(s, mode | ios_base​::​in)[.](#cons-2.sentence-1)

If that function returns a null pointer, callssetstate(failbit)[.](#cons-2.sentence-2)

[ð](#lib:basic_ifstream,constructor__)

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

[3](#cons-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11879)

*Effects*: Equivalent to basic_ifstream(s.c_str(), mode)[.](#cons-3.sentence-1)

[ð](#lib:basic_ifstream,constructor___)

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

[4](#cons-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11891)

*Constraints*: is_same_v<T, filesystem​::​path> is true[.](#cons-4.sentence-1)

[5](#cons-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11895)

*Effects*: Equivalent to basic_ifstream(s.c_str(), mode)[.](#cons-5.sentence-1)

[ð](#lib:basic_ifstream,constructor____)

`basic_ifstream(basic_ifstream&& rhs);
`

[6](#cons-6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11906)

*Effects*: Move constructs the base class, and the contained basic_filebuf[.](#cons-6.sentence-1)

Then calls basic_istream<charT, traits>​::​set_rdbuf(​addressof(*sb*)) to install the contained basic_filebuf[.](#cons-6.sentence-2)

#### [31.10.4.3](#swap) Swap [[ifstream.swap]](ifstream.swap)

[ð](#lib:swap,basic_ifstream)

`void swap(basic_ifstream& rhs);
`

[1](#swap-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11921)

*Effects*: Exchanges the state of *this and rhs by callingbasic_istream<charT, traits>​::​swap(rhs) and*sb*.swap(rhs.*sb*)[.](#swap-1.sentence-1)

[ð](#lib:swap,basic_ifstream_)

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

[2](#swap-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11936)

*Effects*: Equivalent to x.swap(y)[.](#swap-2.sentence-1)

#### [31.10.4.4](#members) Member functions [[ifstream.members]](ifstream.members)

[ð](#lib:rdbuf,basic_ifstream)

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

[1](#members-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11949)

*Returns*: const_cast<basic_filebuf<charT, traits>*>(addressof(*sb*))[.](#members-1.sentence-1)

[ð](#lib:native_handle,basic_ifstream)

`native_handle_type native_handle() const noexcept;
`

[2](#members-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11960)

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

[ð](#lib:is_open,basic_ifstream)

`bool is_open() const;
`

[3](#members-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11971)

*Returns*: rdbuf()->is_open()[.](#members-3.sentence-1)

[ð](#lib:open,basic_ifstream)

`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")
`

[4](#members-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L11984)

*Effects*: Callsrdbuf()->open(s, mode | ios_base​::​in)[.](#members-4.sentence-1)

If that function does not return a null pointer
calls clear(),
otherwise callssetstate(failbit) (which may throwios_base​::​failure) ([[iostate.flags]](iostate.flags "31.5.4.4 Flags functions"))[.](#members-4.sentence-2)

[ð](#lib:open,basic_ifstream_)

`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);
`

[5](#members-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L12003)

*Effects*: Calls open(s.c_str(), mode)[.](#members-5.sentence-1)

[ð](#lib:close,basic_ifstream)

`void close();
`

[6](#members-6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L12014)

*Effects*: Callsrdbuf()->close() and, if that function returns
a null pointer,
callssetstate(failbit) (which may throwios_base​::​failure) ([[iostate.flags]](iostate.flags "31.5.4.4 Flags functions"))[.](#members-6.sentence-1)
