/*----
  signature: main: void -> int
  purpose: to demo some additional file stream features

  by: Sharon Tuttle
  last modified: 2022-09-13
----*/

#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{
    cout << boolalpha;

    ifstream in_stream;
    string in_file;

    cout << "Enter name of file to read from: " << endl;
    cin >> in_file;

    in_stream.open(in_file);

    // complain and exit if the open of that file failed

    if (in_stream.fail())
    {
        cout << "Cannot open " << in_file << " for reading; "
             << endl << "   goodbye!" << endl;
        return EXIT_FAILURE;
    }

    // if reach here, should be safe to read from the file

    // read every line from the file and print each to
    //    the screen

    string next_line;

    while (getline(in_stream, next_line))
    {
        cout << "[" << next_line << "]" << endl;
    }

    in_stream.close();

    // read every "word"/token from file, writing it
    //    to the screen

    in_stream.open(in_file);

    string next_word;

    while (in_stream >> next_word)
    {
        cout << "[" << next_word << "]" << endl;
    }

    in_stream.close();

    return EXIT_SUCCESS;
}