
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib> // For EXIT_FAILURE and atoi

using namespace std;

int main(void)
{
  ifstream InFile;
  string InFileName="Library.csv";
  char str_tmp[100];

  string *Author, *Title;
  int *CallNumber;

  InFile.open(InFileName.c_str());

  if (InFile.fail())
  {
    cerr << "Error - can't open " << InFileName << std::endl;
    exit(EXIT_FAILURE);
  }

  // First count the number of entries
  char c;
  int Lines=0;
  InFile.get(c);
  while(!InFile.eof())
  {
    if (c=='\n')
      Lines++;
    InFile.get(c);
  }
  std::cout << "There are " << Lines << " in " << InFileName << std::endl;

  Author = new string [Lines];
  Title = new string [Lines];
  CallNumber = new int [Lines];


  InFile.clear(); // Clear the eof flag
  InFile.seekg(ios::beg); // rewind to beginning.

  for (int i=0; i< Lines; i++)
  {
     InFile.get(str_tmp, 99, ',');
     Title[i] = str_tmp;
     InFile.ignore();

     InFile.get(str_tmp, 99, ',');
     Author[i] = str_tmp;
     InFile.ignore();

     InFile.get(str_tmp, 99, '\n');
     CallNumber[i] = atoi(str_tmp);
     InFile.ignore();
  }

  std::cout << "Here are the 5133 books: " << std::endl;
  for (int i=0; i< Lines; i++)
  {
     if (CallNumber[i] == 5133)
  	std::cout << setw(20) << Author[i] << ": "
	     << Title[i] << std::endl;
  }

  return(0);
}
