Click here to Skip to main content
15,936,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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";
    }
    //The user gives a path to a directory containing "index"
    //File is used to store filename and fList stores them
    string files = "";
    vector<string> fList;

    //Storing full path of directory to later add /index
    string pathToDir = argv[1];
    string pathToIndex = pathToDir + "index";
    ifstream dirIndex( pathToIndex );

    //Push back all filenames within index to fList
    while( getline( dirIndex, files ) ) {
        fList.push_back( files );
    }

    string thirdWord = "";




    //Iterate through filenames and open them 
    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;
        }

        //Ignore first two lines
        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.
Posted
Updated 27-Sep-17 21:01pm

1 solution

You choosed the appropriate string::find - C++ Reference[^] function but missed that it does not return a boolean value:
Quote:
Return Value
The position of the first character of the first match.
If no matches were found, the function returns string::npos.
So you have to change your code to:
if( goHere.find("/ham/") != string::npos )
 
Share this answer
 
Comments
CPallini 28-Sep-17 3:10am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900