ifstream
From Wikipedia, the free encyclopedia
|
|
It has been suggested that this article or section be merged into fstream. (Discuss) Proposed since May 2009. |
ifstream is the C++ standard library class that provides an interface to read data from files as input streams.[1]
To use the ifstream class, fstream library (header file) must be included using the preprocessor directive: #include <fstream>
The 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.