// Example file demonstrating:
// 1) use of C-style strings in filenames
// 2) passing argc and argv[] to a subroutine
// 3) passing a file handle to a subroutine
// 4) naming and scope of function prototypes, actual arguments, and
//    formal arguments
// 5) C-style struct and typedef
// 6) use of pointers to structs and arrays

// Usage: file_arg [filename]


#include <iostream>  //standard I/O
#include <fstream>   //stream I/O to files
#include <cstdlib>   //see pg 219, exit(), abort(), atexit() defined there
#include <cstring>   //strcpy() defined there
using namespace std;


/* file scope variables */
struct name_element {
 char first_name[20],
      middle_initial,
      last_name[20],
      nickname[20];
};

typedef struct name_element NAME;

/* function prototypes */
void process_arguments (int argc, char* argv[],char filename[], ofstream* data_file);
void write_record      (NAME *person, ofstream* data_file, char filename[]);
ostream& operator<<    (ostream& s, const NAME& person);


/***************************** MAIN PROGRAM ********************************/
/***************************************************************************/
int main(int argc, char* argv[]) {

   ofstream  data_file;
   char      filename[81];
   NAME      student;

   process_arguments(argc, argv, filename, &data_file);

   cout << "File opened is " << filename << endl;

   strcpy(student.first_name,"John");
   student.middle_initial = 'E';
   strcpy(student.last_name,"Boon, Jr.");
   strcpy(student.nickname,"BOON-RA");

   write_record (&student, &data_file, filename);
   // output using overloaded << operator example
   cout << student << endl;

   data_file.close();
   return 0;

}
/***************************************************************************/
/***************************** MAIN PROGRAM ********************************/

/***************************** PROCESS_ARGUMENTS ***************************/

void process_arguments (int argc, char* argv[],
                        char filename[], ofstream* data_file) {
   if (argc < 2) {
      cout << "Please enter a filename (80 char max):  " << endl;
      cin >> filename;
   }
   else
      strcpy(filename, argv[1]);

   data_file->open(filename, ios::out);

   if (!data_file) {
      	cerr << "Cannot open input file" << argv[1] << endl;
      	exit(EXIT_FAILURE);
      }

}

/***************************** WRITE_RECORD ********************************/
void write_record (NAME *person, ofstream* out_file, char filename[]) {

   *out_file << person->first_name << "\t" << person->middle_initial << "\t";
   *out_file << person->last_name  << "\t" << person->nickname << endl;
   return;

}

/***************************** Overload << for NAME *************************/
ostream& operator<< (ostream& s, const NAME& person)
{
   // see Stroustrup, Section 21.2.3 Output of User-Defined Types, pg 612
   return s << person.first_name << " "
            << "(" << person.nickname << ") "
            << person.middle_initial << ". "
            << person.last_name;
}





