// L-6 MCS 360 Fri 3 Sep 2010 : read_file1.cpp

/* The user is prompted for the name of an input file. 

   Unlike Python for example, no exception is generated if the name
   cannot be associated with a file at creation of a file stream.
   Likewise, reading from a file stream unassociated with a file
   does not cause an exception.

We use this example as a motivation for an encapsulation to a file
stream that throws and exception when association to a name fails. */

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

int main()
{
   string input_file_name;

   cout << "Give name of input file : ";
   cin >> input_file_name;

   cout << "Attempting to open \""
        << input_file_name << "\"..." << endl;

   ifstream ins(input_file_name.c_str());

   if(!ins)
      cout << "Opening \"" << input_file_name
           << "\" failed!" << endl;
   else
   {
      cout << "Opening succeeded!"
           << "  Trying to read character ... " << endl;
      char k; 
      ins >> k; 
      cout << "read " << k << endl;
      ins.close();
   }
   return 0;
}

