// ==========================================================================
// $Id: stream_copy.cpp,v 1.1 2006/10/17 22:18:48 jlang Exp $
// CSI2172 example Code for lecture 9
// ==========================================================================
// (C)opyright:
//
//   Jochen Lang
//   SITE, University of Ottawa
//   800 King Edward Ave.
//   Ottawa, On., K1N 6N5
//   Canada. 
//   http://www.site.uottawa.ca
// 
// Creator: jlang (Jochen Lang)
// Email:   jlang@site.uottawa.ca
// ==========================================================================
// $Log: stream_copy.cpp,v $
// Revision 1.1  2006/10/17 22:18:48  jlang
// Added examples for lecture 8
//
// Revision 1.1  2005/11/12 00:05:19  jlang
// Demo code for lecture 9
//
//
// ==========================================================================
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

bool streamCopy( istream& _inS, ostream& _outS ) {
  char curC;
  // check status first
  if ( !_inS || !_outS ) return false;
  _inS.clear(); 
  _outS.clear();
  while( !_inS.eof() ) { 
    _inS.get( curC ); 
    if ( _inS.fail() ) return false;
    _outS << curC;
  }  
  return true;
}


int main( int argc, char *args[] ) {
  if ( argc != 3 ) {
    cerr << "Error: Need 2 filenames: ";
    for ( int i=1; i < argc; i++ ) {
      cerr << " " << args[i];
    }
    cerr << endl;
    return -1;
  }
  string inFileName = args[1]; 
  string outFileName = args[2]; 
  ifstream inFile( inFileName.c_str());
  if ( !inFile ) {
    cerr << "Error: unable to open: " << inFileName << endl;
    return -1;
  }
  ofstream outFile( outFileName.c_str());
  if ( !outFile ) {
    cerr << "Error: unable to open: " << outFileName << endl;
    return -1;
  }
  streamCopy( inFile, outFile );
  outFile.close();

  // Reset infile position
  inFile.clear();
  inFile.seekg(0);
  streamCopy( inFile, cout );
  inFile.close();
  cout << endl;
  return 0;
}
