//============================================================================
// Name        : FileStreams.cpp
// Author      : Clif Presser
//============================================================================

#include <iostream>
#include <fstream>
#include <sstream>



using namespace std;


//read a file word by word (ignore white space and new lines
void streamFileWords (string filename){

        //create and open the file
        ifstream inFile(filename);

        //check for errors
        if(!inFile){
                cerr << "Failed to open file: " << filename << endl;
                return;
        }

        int numWords = 0;

        string word;
        //as long as we haven't gotten to end-of-file (eof)
        while(inFile >> word){
                //read the next word
                numWords += 1;
        }
        cout << numWords << " words." << endl;

        //close the file
        inFile.close();
}

//read a file line by line and process the words of each line
void streamFileLines (string filename){

        //create the stream, open the file
        ifstream inFile(filename);

        //check for errors
        if(!inFile){
                cerr << "Failed to open file: " << filename << endl;
                return;
        }

        int numWords = 0;
        int numLines = 0;

        string line;
        //get the next line
        while(getline(inFile, line)){
                numLines++;

                //create a string stream from it
                stringstream ss(line);
                string word;
                //read words from the string stream.
                while(ss >> word){
                        numWords++;
                }
        }

        cout << numLines << " lines and " << numWords << " words." << endl;

        //close the file
        inFile.close();
}



int main() {
        cout << "Enter a filename: ";
        string filename;
        cin >> filename;

        streamFileWords(filename);
        streamFileLines(filename);


        return 0;
}