/* Lecture code 1.1
 *
 * A program to open a file, then print out all words that aren't integers.
 * To run this program, you'll need to have a file named datafile.dat in the
 * working directory.
 */

#include <iostream>
#include <fstream> // For ifstream
#include <string>  // These next two lines replace genlib.h
using namespace std;

int main()
{
	/* Open the file, failing if unable to do so. */
	ifstream input("datafile.dat");
	if(!input.is_open())
	{
		/* Report an error to cerr, the error-logging stream, then quit with a
		 * nonzero result to signal an error to the operating system.
		 */
		cerr << "Cannot open file!" << endl;
		return -1;
	}
	
	/* To strip out all the integers, we sit in a loop trying to read integers out of the file.  If
	 * we succeed, we simply skip the integer.  Otherwise, we restore stream integrity and read out
	 * the string that blocked the input.
	 */
	while(true)
	{
		/* Try to read an int. */
		int dummy;
		input >> dummy;
		
		/* If this operation failed, one of two possibilities might hold.  First, we might just
		 * be at the end of the file, in which case we are done.  Otherwise, we need to read the
		 * string that gummed up our input.
		 */
		if(input.fail())
		{
			/* Check for end of file; if so, we're done! */
			if(input.eof())	break;
			
			/* Otherwise, we hit a string.  Clear the stream to let us read in more input,
			 * then read out the string and print it to the console.
			 */
			input.clear();
			string word;
			input >> word;
			cout << word << ' ';
		}
	}
	
	return 0;
}