// readUsersFromFile.cpp
// P.Conrad, Spring 2005
// CISC181, A program to read usernames from a file instead of
// from the command line.   It is assumed that the file "usernames.txt"
// is in the "current directory" at the time the program is invoked.
// The output is still written to the screen.

#include <iostream>

using std::cout;
using std::cin;
using std::flush;
using std::endl;
using std::cerr;


#include <fstream> // for reading directly from a disk file
using std::ifstream; // input file stream
using std::ofstream; // for the output file usernames.html

int main(void)
{

  // try to open an input file

  ifstream userInputFile("usernames.txt"); // open the file

  // test whether the file was opened correctly using the
  // overloaded ! operator; returns false if opening the file failed

  if (!userInputFile) 
    {
      cerr << "usernames.txt file could not be opened" << endl;
      exit (-1);
    }
    
  ofstream htmlFile("usernames.html", ios::out);

  if (!htmlFile)
    {
      cerr << "usernames.html file could not be created" << endl;
      exit (-1);  
    }


  // write out the header of the HTML file

  htmlFile << "<HTML>\n<HEAD><TITLE>This lovely page of CISC181 students</TITLE></HEAD>"
	   << endl;
  

  const int MAX_USERNAME_LENGTH = 8; // restriction of 8 comes from Unix rules

  char username[MAX_USERNAME_LENGTH+1];  // +1 because of \0

  int count = 0;

  // try to read first username from input file
  

  userInputFile >> username; // note: userInputFile in place of cin

  while (!userInputFile.eof())  // will stop at end of file, or at CTRL/D
    {
      count ++;
      // convert username into URL and print it out

      cout << " " << count << ") username:" << username << endl;
      
      // get the next username

      userInputFile >> username;
      
    }

  // a parting thought

  cout << "\nThere were " << count << " users in this file\n" << endl;

  return 0;
} 





