Created
March 14, 2010 21:31
-
-
Save tobbez/332252 to your computer and use it in GitHub Desktop.
Word counts in files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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