// demoLineAndChar.cc    Read one character at a time and report line and pos
// P. Conrad for CISC220,06J

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// process one character from the file
// line and pos represent the current line and position in the file

void process(char c, int &line, int &pos);

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


  if (argc != 2)
    {
      cerr << "Usage: " << argv[0] << " inputFileName " << endl;
      exit(1);
    }

  string inputFileName(argv[1]);

  ifstream inf(inputFileName.c_str());

  if (!inf)
    {
      cerr << "Error opening file " << inputFileName << endl;
      exit(2);
    }

  int line=1;
  int pos=1;

  char c;

  inf.get(c);

  while (!inf.eof())
    {
      // process the character c

      process(c, line, pos);
      inf.get(c);
    }

  return 0;
}

void process(char c, int & line, int & pos)
{
  // @@@ in this function, somehow we need to increment line and pos
  // at the appropriate time.

  cout << "I found character '" << c 
       << "' at line " << line 
       << " char " << pos << endl;
  
  if (c=='\n')
    {
      pos = 1;
      line++;
    }
  else
    {
      pos++;
    }

}
