1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
// Function Prototypes
void readFilename(ifstream&, string&);
void countCharsLines(ifstream&, int&, int&, char&);
void populateArray(ifstream&, string[]);
void outputResults(string&, int&, int&, string[]);
int main()
{
// Variables
ifstream inFile;
string filename;
int countLines;
int countChars;
char ch;
string words[1000];
// Function Calls
readFilename(inFile, filename);
countCharsLines(inFile, countLines, countChars, ch);
populateArray(inFile, words);
outputResults(filename, countLines, countChars, words);
return 0;
}
// Function: readFilename
void readFilename(ifstream &inFile, string &filename)
{
cout << "Please enter the file name you wish to open." << endl; // Reads in file name
getline(cin, filename);
inFile.open(filename.c_str()); // Opens file
if (!inFile) // Displays error message
{
cout << "This file did not open properly and the program will now terminate.\nPlease make s\
ure the spelling of the file is correct." << endl;
}
}
// Function: countCharsLines
void countCharsLines(ifstream &inFile, int &countLines, int &countChars, char &ch)
{
string line;
countLines = 0;
countChars = 0;
while(!inFile.eof())
{
getline(inFile,line); // counts lines
countLines++;
}
while(!inFile.eof())
{
ch = inFile.get(); // counts characters
countChars++;
}
inFile.close();
}
// Function: populateArray APPEARS TO BE THE CULPRIT
void populateArray(ifstream &inFile, string words[])
{
for(int i=0;i<=1000;i++)
{
inFile >> words[i]; // Stores the words from the file into the array
}
inFile.close();
}
// Function: outputResults
void outputResults(string &filename, int &countLines, int &countChars, string words[])
{
cout << "Filename: " << filename << endl;
cout << "Number of lines: " << countLines << endl;
cout << "Number of characters: " << countChars << endl;
cout << "Words: " << words << endl;
}
|