Hello,
I currently have a filename that I wish to search for a certain string within, if so, then I wish to do specific tasks based on what substring I found.
Example:
testDir/ham/test1.1
testDir/ham/test2.2
testDir/spam/test3.3
testDir/spam/test4.4
Let's say the filename is stored as a string in "strFilename". Would I be able to use strFilename.find("/ham/") if I wanted to seperate out the HAM and SPAM folders? So far, in my code, using this only returns everything except what I want.
Code:
<pre>int main(int argc, char* argv[] ) {
int wordCount;
int fileStrCount;
int hamCount;
int spamCount;
set<string> seenWords;
map<string, int> ham;
map<string, int> spam;
if( argc < 2 ) {
cout << "ERROR: Must have atleast one filename! \n";
}
string files = "";
vector<string> fList;
string pathToDir = argv[1];
string pathToIndex = pathToDir + "index";
ifstream dirIndex( pathToIndex );
while( getline( dirIndex, files ) ) {
fList.push_back( files );
}
string thirdWord = "";
for(unsigned x = 0; x < fList.size(); x++) {
string goHere = pathToDir + fList[x];
ifstream fileToOpen( goHere );
if( !fileToOpen.is_open() ) {
cout << "Error: " << goHere << " cannot open!\n";
return -1;
}
fileToOpen.ignore( numeric_limits<streamsize>::max(), '\n' );
fileToOpen.ignore( numeric_limits<streamsize>::max(), '\n' );
cout << endl << goHere << endl;
if( goHere.find("/ham/") == true ) {
while( getline( fileToOpen, thirdWord, ' ' ) ) {
++ham[thirdWord];
++hamCount;
wordCount++;
}
} else {
while(getline(fileToOpen, thirdWord, ' ') ) {
++spamCount;
++spam[thirdWord];
wordCount++;
}
}
cout << "Total Words In File: " << fileStrCount << endl;
fileToOpen.close();
fileToOpen.clear();
}
cout << endl << endl << endl << "HAM MAP\n";
for(auto elem : ham)
{
cout << elem.first << " " << elem.second << "\n";
}
cout << endl << endl << endl << "SPAM MAP\n";
for(auto elem : spam)
{
cout << elem.first << " " << elem.second << "\n";
}
}
With a main focus here:
if( goHere.find("/ham/") == true ) {
while( getline( fileToOpen, thirdWord, ' ' ) ) {
++ham[thirdWord];
++hamCount;
wordCount++;
}
} else {
while(getline(fileToOpen, thirdWord, ' ') ) {
++spamCount;
++spam[thirdWord];
wordCount++;
}
}
What I have tried:
I've tried looking into str::substr() but that isn't what I want either.