Please send questions to st10@humboldt.edu .
/*-----
  Signature: main: void -> int

  Purpose: to try to read and print to the screen three integers
           within a file named data.txt in the current working
           directory, and then nuke that file's contents and
           replace them with HA HA
  
  Examples: If the file data.txt contains:
42
13
46
            ...when this program is run, then it will print to the
            screen
42
13
46
            ...and rewrite data.txt so that it ONLY contains
HA HA

  by: Sharon M. Tuttle 
  last modified: 12-03-10
-----*/

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream in_file_stream;       // an input file stream object
    ofstream out_file_stream;      // an output file stream object
    int value;
    string file_to_use = "data.txt";

    // open the input file stream in_file_stream for reading (from physical
    //    file data.txt in the current working directory)
    //
    // (expects an old-style c-string -- use a string instance's
    //    c_str method to get an old-style c-string version of that
    //    string...!)

    in_file_stream.open( file_to_use.c_str() );

    // how you can check (and react) if opening of a file fails:

    if (in_file_stream.fail())
    {
        cout << "file_io_ex: " << file_to_use << " could NOT be "
             << "opened for reading; exiting prematurely." << endl;
        return EXIT_FAILURE;
    }

    cout << "THIS WAS IN data.txt" << endl;

    // this is how you read the next value from the input stream
    //    in_file_stream -- since value is declared as an int, it will
    //    try to convert whatever it reads to an int

    in_file_stream >> value;

    cout << value << endl;

    in_file_stream >> value;
    cout << value << endl;

    in_file_stream >> value;
    cout << value << endl;

    // good style: CLOSE your input stream when you are done!

    in_file_stream.close();

    // open the output file stream out_file_stream for writing (to
    //    physical file data.txt in the current working directory);
    //    when open in this way, any current contents in data.txt
    //    are DELETED!

    out_file_stream.open( file_to_use.c_str() );

    if (out_file_stream.fail())
    {
        cout << "file_io_ex: " << file_to_use << " could NOT be "
             << "opened for writing; exiting prematurely." << endl;
        return EXIT_FAILURE;
    }

    // this is how you write to an output stream

    out_file_stream << "HA HA" << endl;

    // good style: CLOSE your output stream when you are done!

    out_file_stream.close();

    return EXIT_SUCCESS;
}