Hands On with files

Goal: Learn to use files.

Step 1

  1. Create a new project "CountWordsInFile"
  2. Down load EmilyDickinson1.dat. Place in new project folder from previous step.
  3. Write a program to open file, read in a word at a time, and count each word
  4. Display the counts.
  5. Add filtering by stop words: stopwords.txt
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
const string EMILY = "EmilyDickinson1.txt";

// Get words, put into vector
vector<string> getWords(string s);
// Remove special characters, and make everything lower case
string RemoveSpecials(string str);

int main() {
    ifstream poems;
    vector<string> words;
    int wordCount = 0;

    string s;
    while (!poems.fail() && !poems.eof()) {
        getline(poems, s);
        if (s.length() > 0) {
            words = getWords(s);
            wordCount += words.size();
    cout << "Total words: " << wordCount << endl;


vector<string> getWords(string s) {
    istringstream iss(s);
    vector<string> words;
    do {
        string word;
        iss >> word;
        if (word.length() > 0) {
            cout << word << endl;
    } while (iss);

    return words;


string RemoveSpecials(string str)
 int i=0,len=str.length();
 char c=str[i];
 if(((c>='0')&&(c<='9'))||((c>='A')&&(c<='Z'))||((c>='a')&&(c<='z'))||c=='\''||c==' ') 
 if((c>='A')&&(c<='Z')) str[i]+=32; //Assuming dictionary contains small letters only.
 return str;

Step 2

Use count word program code to create a class to count unique words.

Step 3

Output results to file.

Topic attachments
I Attachment History Action Size Date Who Comment
Texttxt stopwords.txt r1 manage 1.9 K 2018-04-17 - 15:19 JimSkon  

This topic: SCMP118 > Main > Fall2017 > PoetryFiles
Topic revision: r2 - 2018-04-17 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback