//Programmer: Bo Bayles  Date: 9 November 2004
//File: lab12.cpp         Class: CS54 C
//Purpose: This program opens a file for input, a file for output, and
//  finds the largest integer in the input file. It outputs that integer to
//  the output file. The user has three chances to enter valid files for
//  both input and output.

#include "fopen.h"
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
//Include cstdlib for exit(), fstream for the file streams, and fopen.h for
//  fileopen.

int main()
{
  fstream infile;
  fstream outfile;
  bool inFail;
  bool outFail;
  int count = 0;
  int largestPos = 0;
  int array[20];
  //Declare in and out files, failure tests, a counter, the largest position
  //  indicator, and the array of integers.

  inFail = fileopen(infile, "input");
  if (inFail == false)
  {
	cout << endl << "Failure opening file." << endl;
    exit(0);
  }
  //Call fileopen for input. If it returns false, exit immediately.

  outFail = fileopen(outfile, "output");
  if (outFail == false)
  {
	cout << endl << "Failure opening file." << endl;
    exit(0);
  }
  //Call fileopen for output. If it returns false, exit immediately.

  infile >> array[count];
  while(!infile.eof())
  {
    infile >> array[++count];
    if (infile.eof())
      break;
  }
  //Read each integer into the array, making sure not to go past the end
  //  of the file.

  for (int j = 0; j < count; j++)
  {
    if (array[j] > array[largestPos])
      largestPos = j;
  }
  //If the integer in the jth position is larger than what is currently set as
  //  the largest position, change largestPos to j.

  outfile << array[largestPos] << endl;
  //Output the position of the array found to have the largest integer.

  infile.close();
  outfile.close();
  //Close the files.

  return 0;
}

