basic_ifstream::basic_ifstream

Constructs an object of type basic_ifstream.

basic_ifstream( );
explicit basic_ifstream(
    const char *_Filename,
    ios_base::openmode _Mode = ios_base::in,
    int _Prot = (int)ios_base::_Openprot
);
explicit basic_ifstream(
    const wchar_t *_Filename,
    ios_base::openmode _Mode = ios_base::in,
    int _Prot = (int)ios_base::_Openprot
);
basic_ifstream(basic_ifstream&& right);

Parameters

  • _Filename
    The name of the file to open.

  • _Mode
    One of the enumerations in ios_base::openmode.

  • _Prot
    The default file opening protection, equivalent to the shflag parameter in _fsopen, _wfsopen.

Remarks

The first constructor initializes the base class by calling basic_istream(sb), where sb is the stored object of class basic_filebuf<Elem, Tr>. It also initializes sb by calling basic_filebuf<Elem, Tr>.

The second and third constructors initializes the base class by calling basic_istream(sb). It also initializes sb by calling basic_filebuf<Elem, Tr>, then sb.open(_Filename, _Mode | ios_base::in). If the latter function returns a null pointer, the constructor calls setstate(failbit).

The fourth constructor initializes the object with the contents of right, treated as an rvalue reference.

Example

The following example shows how to read in text from a file. To create the file, see the example for basic_ofstream::basic_ofstream.

// basic_ifstream_ctor.cpp
// compile with: /EHsc

#include <fstream>
#include <iostream>

using namespace std;

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

Input: basic_ifstream_ctor.txt

This is the contents of basic_ifstream_ctor.txt.

Output

This is the contents of basic_ifstream_ctor.txt.

Requirements

Header: <fstream>

Namespace: std

See Also

Reference

basic_ifstream Class

iostream Programming

iostreams Conventions