Share via


basic_ifstream Class

Describes an object that controls extraction of elements and encoded objects from a stream buffer of class basic_filebuf<Elem, Tr>, with elements of type Elem, whose character traits are determined by the class Tr.

template <class Elem, class Tr = char_traits<Elem> >
    class basic_ifstream : public basic_istream<Elem, Tr>

Parameters

  • Elem
    The basic element of the file buffer.

  • Tr
    The traits of the basic element of the file buffer (usually char_traits<Elem>).

Remarks

The object stores an object of class basic_filebuf<Elem, Tr>.

Example

The following example shows how to read in text from a file.

// basic_ifstream_class.cpp
// compile with: /EHsc

#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    ifstream ifs("basic_ifstream_class.txt");
    if (!ifs.bad())
    {
        // Dump the contents of the file to cout.
        cout << ifs.rdbuf();
        ifs.close();
    }
}

Input: basic_ifstream_class.txt

This is the contents of basic_ifstream_class.txt.

Output

This is the contents of basic_ifstream_class.txt.

Constructors

basic_ifstream

Initializes a new instance of a basic_ifstream object.

Member Functions

close

Closes a file.

is_open

Determines if a file is open.

open

Opens a file.

rdbuf

Returns the address of the stored stream buffer.

swap

Exchanges the content of this basic_ifstream for the content of the provided basic_ifstream.

Operators

operator=

Assigns the content of this stream object. This is a move assignment involving an rvalue that does not leave a copy behind.

Requirements

Header: <fstream>

Namespace: std

See Also

Reference

Thread Safety in the C++ Standard Library

iostream Programming

iostreams Conventions