/*
The following program reads the name of an input file and then it counts the number of lines in the file. 
Note that this method is not guaranteed ti give the exact number of lines (e.g. if the user didn't hit enter
after typing the last line, the number of lines counted by this method will be 1 less than the actual number
of lines.

The program also counts the number of semicolons in the file and the number of spaces.

It writes the three counts above to an output file whose name is entered by the user.

*/
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string inFileName, outFileName;
	ifstream inFile;
	ofstream outFile;
	int count_semicolons = 0, count_lines = 0, count_spaces = 0;
	char ch;

	cout << "Enter name of input file: ";
	cin >> inFileName;

	inFile.open(inFileName.c_str());
	if (!inFile)
	{
		cout << "Can't open file " << inFileName << endl;
		return 1;
	}

	inFile.get(ch);
	while (inFile)
	{
		if (ch == ';')
			count_semicolons++;
		else if (ch == '\n')
			count_lines++;
		else if (ch == ' ')
			count_spaces++;
		inFile.get(ch);
	}

	inFile.close();
	
	cout << "Enter name of output file: ";
	cin >> outFileName;

	outFile.open(outFileName.c_str());
	if (!outFile)
	{
		cout << "Can't open file " << outFileName << endl;
		return 1;
	}

	outFile << "There are " << count_semicolons << " semicolons "
			<< "in file " << inFileName << ".\n";

	outFile << "There are " << count_lines << " lines "
			<< "in file " << inFileName << ".\n";
	
	outFile << "There are " << count_spaces << " spaces "
			<< "in file " << inFileName << ".\n";

	outFile.close();
	return 0;
}
