Skip to content

Instantly share code, notes, and snippets.

@tobbez
Created March 14, 2010 21:31
Show Gist options
  • Save tobbez/332252 to your computer and use it in GitHub Desktop.
Save tobbez/332252 to your computer and use it in GitHub Desktop.
Word counts in files
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
class FileChecker {
private:
map<string, unsigned> keywords;
public:
FileChecker(const string &keyword_filename)
{
ifstream kwfile(keyword_filename.c_str());
string tmp;
while (kwfile >> tmp) {
keywords[tmp] = 0;
}
kwfile.close();
}
void check(const string &filename)
{
for (map<string, unsigned>::iterator it = keywords.begin(); it != keywords.end(); ++it) {
it->second = 0;
}
ifstream file(filename.c_str());
string tmp;
while (file >> tmp) {
if (keywords.find(tmp) != keywords.end()) {
keywords[tmp]++;
}
}
file.close();
cout << filename << ":" << endl;
for (map<string, unsigned>::iterator it = keywords.begin(); it != keywords.end(); ++it) {
cout << " " << it->first << ": " << it->second << endl;
}
cout << endl;
}
};
int main()
{
FileChecker fc("keywords.txt");
fc.check("ling/3-1msg1.txt");
fc.check("ling/3-1msg1.txt");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment