// readAttendanceFile.cc


#include <iostream>
#include <fstream>
#include <cstring> // for strtok and for strlen
using namespace std;

struct Student_S
{
  char *username;
  int areaCode;
  char *firstname;
};


void printStudent(const Student_S & s)
{
      cout << "username: " << s.username << endl
	   << "areacode: " << s.areaCode << endl
	   << "firstname: " << s.firstname << endl;
}

const int MAX_NUM_STUDENTS = 60;

const int MAXBUF = 1024;

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

  // check command line arguments

  if (argc!=2)
    {
      cerr << "Usage: " << argv[0] << " inputFileName " << endl;
      exit(1);
    }
  
  // try to open the input file

  ifstream infile (argv[1], ios::in);

  if (!infile)
    {
      cerr << "Could not open the file: " <<  argv[1] << endl;
      exit(2);
    }

  // declare an array of structs

  Student_S students[MAX_NUM_STUDENTS];

  // as a warm up, just try reading two lines from the file
  // into students[0] and students[1].

  char buf[MAXBUF];
  
  // the next three variables are NOT places to store something.
  // they are just pointers into the array known as "buf"
  // they mark the starting positions of the three parts of the input line

  char *theUsername;
  char *theAreaCodeAsString;
  char *theFirstName;

  int count = 0;

  infile.getline(buf,MAXBUF); // read up to 1024 chars, or until \n

  while (!infile.eof() && !infile.fail())
    {
      // use strtok to break up buf into individual fields
      
      cout << "buffer: |" << buf << "|" << endl;
      
      theUsername = strtok(buf,",");
      theAreaCodeAsString = strtok(NULL,",");
      theFirstName = strtok(NULL,",");

      
      students[count].username = new char[strlen(theUsername) + 1];
      strcpy(students[count].username,theUsername);

      students[count].areaCode = atoi(theAreaCodeAsString);

      students[count].firstname = new char[strlen(theFirstName) + 1];
      strcpy(students[count].firstname,theFirstName);
      


      count++;

      infile.getline(buf,MAXBUF); // read up to 1024 chars, or until \n

    }
  
  for (int i=0; i<count; i++)
    printStudent(students[i]);

  return 0;

  

}








