ifstream

From Wikipedia, the free encyclopedia


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 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.

[edit] References

Tutorial from CPlusPlus.com