So I have several text files. I need to figure out the 10 most common characters and words in the file. I've decided to use a vector, and load it with each character from the file. However, it needs to include white space and new lines.
This is my current function
void readText(ifstream& in1, vector<char> & list, int & spaces, int & words)
{
//Fills the list vector with each individual character from the text ifle
in1.open("test1");
in1.seekg(0, ios::beg);
std::streampos fileSize = in1.tellg();
list.resize(fileSize);
string temp;
char ch;
while (in1.get(ch))
{
//calculates words
switch(ch)
{
case ' ':
spaces++;
words++;
break;
default:
break;
}
list.push_back(ch);
}
in1.close();
}
But for some reason, it doesn't seem to properly hold all of the characters. I have another vector elsewhere in the program that has 256 ints all set to 0. It goes through the vector with the text in it and tallys up the characters with their 0-256 int value in the other vector. However, it's tallying them up fine but spaces and newlines are causing problems. Is there a more efficient way of doing this?