public member function
<fstream>

std::basic_ifstream::basic_ifstream

default (1)
basic_ifstream();
initialization (2)
explicit basic_ifstream (const char* filename,                         ios_base::openmode mode = ios_base::in);
default (1)
basic_ifstream();
initialization (2)
explicit basic_ifstream (const char* filename,                         ios_base::openmode mode = ios_base::in);explicit basic_ifstream (const string& filename,                         ios_base::openmode mode = ios_base::in);
copy (3)
basic_ifstream (const basic_ifstream&) = delete;
move (4)
basic_ifstream (basic_ifstream&& x);
Construct object
Constructs a basic_ifstream object:

(1) default constructor
Constructs a basic_ifstream object that is not associated with any file.
Internally, its basic_istream base constructor is passed a pointer to a newly constructed basic_filebuf object (the internal file stream buffer).
(2) initialization constructor
Constructs a basic_ifstream object, initially associated with the file identified by its first argument (filename), open with the mode specified by mode.
Internally, its basic_istream base constructor is passed a pointer to a newly constructed basic_filebuf object (the internal file stream buffer). Then, basic_filebuf::open is called with filename and mode as arguments.
If the file cannot be opened, the stream's failbit flag is set.
(3) copy constructor (deleted)
Deleted (no copy constructor).
(4) move constructor
Acquires the contents of x.
First, the function move-constructs both its base basic_istream class from x and a basic_filebuf object from x's internal basic_filebuf object, and then associates them by calling member set_rdbuf.
x is left in an unspecified but valid state.
The internal basic_filebuf object has at least the same duration as the basic_ifstream object.

Parameters

filename
A string representing the name of the file to open.
Specifics about its format and validity depend on the library implementation and running environment.
mode
Flags describing the requested i/o mode for the file.
This is an object of the bitmask member type openmode that consists of a combination of the following member constants:
member constantstands foraccess
in *inputFile open for reading: the internal stream buffer supports input operations.
outoutputFile open for writing: the internal stream buffer supports output operations.
binarybinaryOperations are performed in binary mode rather than text.
ateat endThe output position starts at the end of the file.
appappendAll output operations happen at the end of the file, appending to its existing contents.
trunctruncateAny contents that existed in the file before it is open are discarded.
These flags can be combined with the bitwise OR operator (|).
* in is always set for basic_ifstream objects (even if explicitly not set in argument mode).
Note that even though basic_ifstream is an input stream, its internal basic_filebuf object may be set to also support output operations.

If the mode has app set, the opening operation fails. It also fails if trunc is set but out is not.
If the mode has both trunc and app set, the opening operation fails. It also fails if trunc is set but out is not.
x
A basic_ifstream object of the same type (with the same class template parameters charT and traits), whose value is moved.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// ifstream constructor.
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream

int main () {

  std::ifstream ifs ("test.txt", std::ifstream::in);

  char c = ifs.get();

  while (ifs.good()) {
    std::cout << c;
    c = ifs.get();
  }

  ifs.close();

  return 0;
}

Data races

The move constructor (4) modifies x.

Exception safety

-

See also