SOURCE FILE: 02_read_intsB.cpp



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

/*

*/

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 next_int ;

     /* If the file is not empty, the 'priming read' below, reads the first
        value from the file and puts a copy of the value into this
        variable:  next_int.  If the file is empty, the attempt to read
        causes the 'end-of-file condition' (eof) to become true.  */

  fin >> next_int ; // the 'priming read'
  while ( !fin.eof() )  // while NOT eof in fin
  {
          // print the value that was last read from fin
     cout << next_int << endl ; 
 
          /* Now attempt to read the next value.  If there is no next
             value, this attempt causes eof to become true.  */
     fin >> next_int ;
  }
  fin.close() ;
  cout << endl ;
  cout << "\nAll Done!!\n\n" ;
  return 0 ;
}