Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
IOstream Library
manipulators
classes:
filebuf
fstream
ifstream
ios
iostream
ios_base
istream
istringstream
ofstream
ostream
ostringstream
streambuf
stringbuf
stringstream
objects:
cerr
cin
clog
cout
types:
fpos
streamoff
streampos
streamsize
ifstream
ifstream::ifstream
member functions:
ifstream::close
ifstream::is_open
ifstream::open
ifstream::rdbuf


ifstream::ifstream

constructor member
ifstream ( );
explicit ifstream ( const char * filename, ios_base::openmode mode = ios_base::in );

Construct object and optionally open file

Constructs an object of the ifstream class. This implies the initialization of the associated filebuf object and the call to the constructor of its base class with the filebuf object as parameter.

Additionally, when the second constructor version is used, the stream is associated with a physical file as if a call to the member function open with the same parameters was made.

If the constructor is not successful in opening the file, the object is still created although no file is associated to the stream buffer and the stream's failbit is set (which can be checked with inherited member fail).

Parameters

filename
C-string containing the name of the file to be opened.
mode
Flags describing the requested i/o mode for the file. This is an object of type ios_base::openmode, which consists on a combination of one or more of the following flags defined as member constants:
flag valueopening mode
app(append) Set the stream's position indicator to the end of the stream before each output operation.
ate(at end) Set the stream's position indicator to the end of the stream on opening.
binary(binary) Consider stream as binary rather than text.
in(input) Allow input operations on the stream.
out(output) Allow output operations on the stream.
trunc(truncate) Any current content is discarded, assuming a length of zero on opening.

Return Value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// ifstream constructor.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
  ifstream ifs ( "test.txt" , ifstream::in );
  while (ifs.good())
    cout << (char) ifs.get();
  ifs.close();
  return 0;
}


This example opens a file and prints its content character by character to the standard output.

Basic template member declaration

( basic_ifstream<charT,traits> )
1
2
basic_ifstream();
explicit basic_ifstream  ( const char * filename, ios_base::openmode mode = ios_base::in );


See also