Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // File name: WordCount.cpp;
- // Author: Grétar Már Margrétarson;
- // E-mail: gretarm11@ru.is;
- // Assaignment: 3;
- // Description: A program that counts words, characters and lines and then writes out to another file and to the screen.
- // Last changed: 13. október, 2011;
- #include <fstream>
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- void GetFiles(ifstream& fin, ofstream& fout);
- void OpenFiles(ifstream& fin, ofstream& fout);
- void LineCount(ifstream& fin, ofstream& fout, int& lineCount);
- void WordCount(ifstream& fin, ofstream& fout, int& wordCount);
- void CharCount(ifstream& fin, ofstream& fout, int& charCount);
- void DisplayResults(ostream& fout, int lineCount, int wordCount, int charCount);
- void CloseFiles(ifstream& fin, ofstream& fout);
- int main()
- {
- int lineCount = 0, wordCount = 0, charCount = 0;
- ifstream fin;
- ofstream fout;
- GetFiles(fin, fout);
- OpenFiles(fin, fout);
- //LineCount(fin, fout, lineCount);
- //WordCount(fin, fout, wordCount);
- CharCount(fin, fout, charCount);
- CloseFiles(fin, fout);
- DisplayResults(fout, lineCount, wordCount, charCount);
- DisplayResults(cout, lineCount, wordCount, charCount);
- CloseFiles(fin, fout);
- return 0;
- }
- void GetFiles(ifstream& fin, ofstream& fout)
- {
- char inFile[16], outFile[16];
- cout << "Enter the name of your input and then output file (max 15 characters each): ";
- cin >> inFile >> outFile;
- cout << "You have chosen the file: " << inFile << " as input.\n" << "You have chosen the file: " << outFile << " as output.";
- }
- void OpenFiles(ifstream& fin, ofstream& fout)
- {
- char inFile[16], outFile[16];
- fin.open(inFile);
- if (fin.fail())
- {
- cout << "Input file opening failed.";
- exit(1);
- }
- fout.open(outFile);
- if (fout.fail())
- {
- cout << "Output file opening failed.";
- exit(1);
- }
- }
- void LineCount(ifstream& fin, ofstream& fout, int& lineCount)
- {
- }
- void WordCount(ifstream& fin, ofstream& fout, int& wordCount)
- {
- char next;
- fin.get(next);
- while (! fin.eof())
- {
- if (next == " " || next == "\n" || next == "\t" || next == "\r")
- {
- wordCount++;
- }
- }
- }
- void CharCount(ifstream& fin, ofstream& fout, int& charCount)
- {
- char next;
- fin.get(next);
- while (! fin.eof())
- {
- charCount++;
- }
- }
- void DisplayResults(ostream& fout, int lineCount, int wordCount, int charCount)
- {
- fout << "The input contains: \n" << lineCount << " lines\n" << wordCount << " words \n" << charCount << " characters" << endl;
- }
- void CloseFiles(ifstream& fin, ofstream& fout)
- {
- fin.close();
- fout.close();
- cin.get();
- cin.get();
- }
Add Comment
Please, Sign In to add comment