// readUsersFromFile.cpp
// P.Conrad, Spring 2006
// 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::cerr;
using std::endl;


#include <fstream> // for reading directly from a disk file
using std::ifstream; // input file stream


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);
    }
    

  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;
} 

