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
|
#include<iostream>
#include<fstream>
using namespace std;
// Function Prototypes
void readFilename(ifstream&, string&);
void countNumberLines(ifstream&, int&);
void countNumberChars(ifstream&, int&);
void populateArray(ifstream&, string[]);
int main()
{
// Variables
ifstream inFile;
string filename;
int countLines;
int countChars;
char ch;
string words[1000];
// Function Calls
readFilename(inFile, filename);
countNumberLines(inFile, countLines);
countNumberChars(inFile, countChars);
populateArray(inFile, words);
return 0;
}
// Function: readFilename
void readFilename(ifstream &inFile, string &filename)
{
// Reads in file name.
cout << "Please enter the file name you wish to open." << endl;
getline(cin, filename);
inFile.open(filename.c_str());
// Displays error message if file is not found.
if (!inFile)
{
cout << "This file did not open properly and the program will now terminate.\nPlease make sure 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);
countLines++;
}
inFile.clear();
inFile.seekg(0, ios::beg);
while(!inFile.eof())
{
ch = inFile.get();
countChars++;
}
inFile.close();
}
// Function: populateArray
void populateArray(ifstream &inFile, string words[])
{
for(int i=0;i<=1000;i++)
{
inFile >> words[i];
}
inFile.close();
}
|