// From Brian Harrington's 2050 class
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;


int main(int argc, char *argv[])
{
        // Check input
        if(argc<2)
        {
                cout<<"Usage: "<<argv[0]<<" <filename>"<<endl;
                return 0;
        }

        // Try to read from file
        cout<<"Reading tokens from file '"<<argv[1]<<"':"<<endl;
        ifstream in(argv[1]);
        if(!in)
                cout<<" - Could not read from file '"<<argv[1]<<"'."<<endl;
        else
        {
                string token;
                cout.setf(ios::right);
                for(unsigned i=1; in>>token; i++)
                        cout<<setw(4)<<i<<": "<<token<<endl;
        }
        in.close();
        cout<<endl;

        // Allow user to enter a token
        string text;
        cout<<"Enter some text: ";
        getline(cin, text);

        // Append new tokens to file
        ofstream out(argv[1], ios::app);
        if(out)
                out<<endl<<text<<endl;
        else
                cout<<"- Could not write to file '"<<argv[1]<<"'"<<endl;
        out.close();

        return 0;
}


