Dev C++ Ifstream

The C++ classes ifstream and ofstream are subclasses of istream and ostream designed to perform stream input and output to disk files. You can use the same extractors and inserters on ifstream and ofstream objects that you’ve been using on cin and cout.

The ifstream is actually an instantiation of the template class basic_ifstream<T> with T set to char. The basic_ifstream<T> template class is instantiated with other types as well to provide different types of input classes. For example, the wide stream file class wifstream is based on the same basic_ifstream<T> with T set to wchar_t. The ofstream is the same as basic_ofstream<char>.

The classes ifstream and ofstream provide constructors used to open a file for input and output, respectively:

@AlKepp: the question is also tagged with ifstream, and the OP has mentioned in comments that he is also trying to use ifstream to display the file, so I included use of ifstream in my answer – Remy Lebeau Mar 29 '18 at 19:42.

C++

The first argument is a pointer to the name of the file to open. The second argument specifies the mode. The type openmode is an integer type defined in ios_base. Also defined within ios_base are the possible values for mode listed in this table.

These are bit fields that the programmer bitwise ORs together. The default mode for ifstream is to open the file for input with the pointer set to the beginning of the file (that’s logical enough).

Dev C++ Iostream No Such File

Constants that Control How Files Are Opened
FlagMeaning
ios_base::appSeek to end-of-file before each write.
ios_base::ateSeek to end-of-file immediately after opening the file, if it
exists.
ios_base::binaryOpen file in binary mode (alternative is text mode).
ios_base::inOpen file for input (implied for istream).
ios_base::outOpen file for output (implied for ostream).
ios_base::truncTruncate file, if it exists (default for ostream).

The default for ofstream is to open for output and to truncate the file if it exists already. The alternative to truncate is ios_base::app, which means append new output onto the end of the file if it exists already. Both options create a file if it doesn’t already exist.

Dev C++ Iostream.h

For example, the following StreamOutput program opens the file MyName.txt and then writes some important and absolutely true information to that file:

Dev C++ Iostream H Download

The destructor for the file stream classes automatically close the associated file. In this simple example, the MyName.txt file was closed when the my object went out of scope upon returning from main(). Global objects are closed as part of program termination.