/* 
 * 
 * CSI2131
 * 
 * Jeff Souza 
 * 
 * Lab10
 * 
 */

#include "student.h"
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>     // for setw, setiosflags, etc...
using namespace std;

// the number of students to read in
const int num_students = 10;
const int number_length = 7;
const int name_length = 21;

// set the record size, which is the sum of the lengths
// of the student number field, the name field, and 
// extra end-of-line characters 
const int recsize = number_length + name_length + 2;


// read a student from a istream 
void readStudent(int, istream&, Student&);
// write a student in the ostream
void writeStudent(ostream&, Student);

// seek to the appropriate position for reading
void InputPositioningByRRN(int RRN, istream&);
// seek to the appropriate position for writing
void OutputPositioningByRRN(int RRN, ostream&);
// modify the name of a student stored in a file 
void ModifyStudentName(int, ostream&, string);


// main method
int main( )
{
  Student student;					  // a students
 
  fstream input;                      // input stream
  fstream output;                     // output stream

  char inputname[40];                 // input filename
  char outputname[40];                // output filename

  cout << "What is the input file name? ";
  cin >> inputname;

  cout << "What is the output file name? ";
  cin >> outputname;

  
  // open the input file for input, of course
  input.open(inputname, ios::in);

  // Check to make sure that we could open the files properly!
  if (input.fail()) {
    cerr << "Could not open file ";
    cerr << inputname;
    cerr << ".\n";
    return 1;
  }


  // open the output for output
  output.open(outputname, ios::out);

  // read students in reverse order from input and
  // write them in the output
  for (int i=(num_students-1);i>=0;i--) {
    readStudent(i, input, student);
    writeStudent(output, student);
  }

  // modify the name of a student 
  ModifyStudentName(4, output, "New John Paul");

  cout << endl << "Program completed successfully !!" << endl;
  cout << "Check output file out." << endl << endl;

  input.close();
  output.close();

  return 0;
}

// read a student from a istream 
void readStudent(int rrn, istream& input, Student& s)
{
  string numberstr;
  string namestr;
  long int number;
  string line;

  // seek to the appropriate student
  InputPositioningByRRN(rrn, input);

  // read in a line from fine
  getline(input,line,'\n');

  // get the number and name strings
  numberstr = line.substr(0,number_length);
  namestr = line.substr(number_length,number_length+name_length);

  // convert the number string into a long int.
  number = atol((numberstr.c_str()));

  // set number and name for student s
  s.setNumber(number);
  s.setName(namestr);
}

// write a student in the ostream
void writeStudent(ostream& output, Student s)
{
  output << s.getNumber() << s.getName() << endl;
}

// seek to the appropriate record for reading
void InputPositioningByRRN(int RRN, istream& input)
{
  input.seekg(RRN * recsize, ios::beg);
}

// seek to the appropriate position for writing
void OutputPositioningByRRN(int RRN, ostream& output)
{
  output.seekp(RRN * recsize, ios::beg);
}


// modify the name of a student stored in a file 
void ModifyStudentName(int rrn, ostream& output, string newname){
   // seek to the appropriate student
   OutputPositioningByRRN(rrn,output);
   // seek to the name field
   output.seekp(number_length, ios::cur);
   // write the new name over the old one
   output << setw(name_length) << setiosflags(ios::left) << newname << endl;
}




