C++ has three options to read data from a file — ifstream, ofstream and fstream. To input, or read data, use ifstream. To output or write data, use ofstream. The most versatile, fstream, allows both reading and writing data.
For this program, I was asked to count the frequency each letter of the alphabet appears in a specific text file.
Reference variables, denoted with an ampersand, proved useful during this assignment. Reference variables allow you to create a function parameter references the actual variable passed to it, instead of copying the data to parameter variable.
Also, I decided to use the fstream to preform this task. Since fstream is versatile, in the open member function, you can pass ios::in or ios::out as an argument, clarifying whether to input or output data.
[code language=”cpp”] /***** Input File Text: ****
This is an input file being used to learn C plus plus.
The letter "e" is supposed to be the most frequently used letter in the
English prose, and the letter "z" is the least used. This program will
print each letter of the English alphabet and the number of times a letter
is used.
*/
#include <iostream>
//Include fstream library, used for both input and output
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
//Define a void function prototype to read count how many times each letter appears
//Input stream variable passed by reference and char variable
void countLetters(fstream &inputFile, char);
//
int main()
{
//Declare string for file name and line, fstream object, Declare char
string fileName = "homework.txt";
string line;
char ch;
//Output user information
cout << "******************* Following is data in the input file "
<< "*****************\n" << endl;
//Creates an fstream object for the program
fstream inputFile;
/*
– Open the file and pass its name to fstream object function open
– fstream input, must use ios::in
– fstream output, must use ios::out
– ios::in and ios::out are "file open modes," I will pass them
as a second argument of open member functions
*/
inputFile.open("homework.txt", ios::in);
/*
– If file not opened output message, pause execution, exit the program
– Error detection, checks if open fails,
uses the ! operator to test stream status
*/
if (!inputFile)
{
cout << "The file " << fileName << " could not be opened.\n\n\n\n";
system("pause");
exit(1);
}
//Output the contents of the file with a loop
while(getline(inputFile, line))
{
cout << line << endl;
}
cout << "\n\n";
//Output info telling the user what the data is
cout << "Letter frequencies in the input file are as follows:\n\n";
//Call the letter counting function
countLetters(inputFile, ch);
//Close the file
inputFile.close();
//Pause to view the output
cout << "\n\n\n\n";
system("pause");
return 0;
}
/* Function definition, with parameter that is a reference variable, referencing
the original variable, instead of just copying the data of the variable */
void countLetters(fstream &inputFile, char ch)
{
char compare = 65;
int count;
for (int i = 0; i < 26; i++)
{
count = 0;
inputFile.clear();
inputFile.seekg(0, ios::beg);
ch = inputFile.get();
while (ch != EOF)
{
if (toupper(ch) == compare)
{
count++;
}
ch = inputFile.get();
}
cout << compare << " : " << count << "\n";
compare++;
}
}
[/code]
No Comments Yet