SOURCE FILE: 03_my_cat.cpp



#include <iostream>
#include<fstream>
//#include <cmath>
using namespace std ;

/*
     This program is designed to get a file name from the user and then copy
     all of the characters in the file to the cout stream (the screen, by
     default).  It also counts the characters in the file and prints out the
     count at the end of the output.

     The program will "error out" if it cannot open the file specified by the
     user.
*/

int main ()
{
  char filename[256] ;

  cout << "Please type the name of a file: " ;
  cin >> filename ;

  ifstream fin ;
  fin.open(filename);
  if (fin.fail())
  {
    cout << "FAILED TO OPEN FILE: " << filename << endl ;
    exit(1) ;
  }

  int numCharsPrinted = 0 ;

  char next_char ;

        /* Copy the first character in the file (if it exists) into this
           variable: next_char */
  fin.get(next_char) ; 
       //  fin >> next_char ; // This would not work correctly.  Why?
  while ( !fin.eof() )
  {
    cout << next_char ; // write the latest character 
    numCharsPrinted++ ;  
    fin.get(next_char) ;
    //  fin >> next_char ;
  }

  fin.close() ;

  cout << "\n\nThere were " << numCharsPrinted 
                  << " characters in the file.\n" ;
  cout << "All Done!!\n\n" ;
  return 0 ;
}