- Ifstream
ifstream is the
C++ standard library class that provides an interface to read data from files as input streams.To use the ifstream class,
fstream library (header file) must be included using thepreprocessor directive: #includeThe input stream may open a file in the constructor:
ifstream inf("input.dat", ifstream::in);
or afterwards:
ifstream inf;inf.open("input.dat");
To close a stream, one uses the
close
method:inf.close();
The ifstream destructor will close the file cleanly as well. It is perfectly acceptable to allow the ifstream object to fall out of scope without calling
close
. This is often a desirable style, as it simplifies the "clean up" process when an exception is thrown or an error is otherwise encountered.References
[http://www.cplusplus.com/reference/iostream/ifstream/ Tutorial from CPlusPlus.com]
Wikimedia Foundation. 2010.