Adding File Input and Output

Unless data can be stored safely, it will disappear when the program exits. Therefore, it is necessary to write to and read from files. Just like the console <iostream> interface, the file uses <fstream>. It needs to be included with your other standard library resources. A related library that is sometimes needed is <sstream>.

#include <fstream>
#include <sstream>

When a file is open for reading or writing, be sure to close it when done. The methods/functions in fstream are a part of a class. The three most used classes are:

  1. ifstream – for input
  2. ofstream – for output
  3. fstream – for a file that can be used for either, be careful of this.

I used the rainfall program that was done in our class to show how to use File I/O.

Please let me know if you have any questions.

I would enjoy hearing from you.

This site uses Akismet to reduce spam. Learn how your comment data is processed.