How to search a document for a string in C ++?

Here is my code:

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int main()
{
    int count = 0;
    string fileName;
    string keyWord;
    string word;


    cout << "Please make sure the document is in the same file as the program, thank you!" 
         << endl << "Please input document name: " ;
    getline(cin, fileName);
    cout << endl;

    cout << "Please input the word you'd like to search for: " << endl;
    cin >> keyWord;
    cout << endl;
    ifstream infile(fileName.c_str());
    while(infile.is_open())
    {
        getline(cin,word);
        if(word == keyWord)
        {
            cout << word << endl;
            count++;
        }
        if(infile.eof())
        {
            infile.close();
        }

    }
    cout << count;

}

I'm not sure how to move on to the next word, these are endless loops now ... any recommendation?

Also ... how can I say to print the line that the word was in?

Thanks in advance!

+3
source share
4 answers
while(infile >> word)
{
    if(word == keyWord)
    {
        cout << word << endl;
        count++;
    }
}

This will do the job. Please read about streams more.

+7
source

If all you want to do is count the number of keywords in a file, then:

int count = std::count(std::istream_iterator<std::string>(infile),
                       std::istream_iterator<std::string>(),
                       keyword);

If you want to read the words.
But also want to print line numbers, then something like this should work:

std::string      line;
std::ifstream    infile("plop");
int              lineNumber = 0;

while(std::getline(infile, line)) 
{
    ++lineNumber ;
    std::stringstream   linestream(line);
    int hits = std::count(std::istream_iterator<std::string>(linestream),
                          std::istream_iterator<std::string>(),
                          keyword);
    if (hits != 0)
    {
        std::cout << "Line: " << lineNumber << "   Matches(" << hits << ")\n";
    } 
    count  += hits;
} 
+2
source

while(infile.is_open()) while(infile). eof .

- , . , , failbit (getline ), eof , , . operator bool .

0

:

getline(cin,word);

if(word == keyWord)
{
    cout << word << endl;
    count++;
}

, cin. infile. :

infile >> word;
if(word == keyWord)
    {
        cout << word << endl;
        count++;
    }

In addition, you must change the condition of the loop. You do not need to check if infile exists . You must check this before starting the cycle. For the loop, you need to check if the eof state is reached or not:

if ( !infile.is_open() ) {
    cerr << "Error while opening file." << endl;
    exit( EXIT_FAILURE );
}    

while( !infile.eof() ) {
    infile >> word;
    if(word == keyWord)
    {
        cout << word << endl;
        count++;
    }
}

And as you can see, now you can get rid of this strange second if you put it inside the loop.
The final step is to introduce the โ€œread aheadโ€ technique: it makes no sense to test if we are not reading anything.

if ( !infile.is_open() ) {
    cerr << "Error while opening file." << endl;
    exit( EXIT_FAILURE );
}    

infile >> word;    
while( !infile.eof() ) {
    if( word == keyWord )
    {
        cout << word << endl;
        count++;
    }

    infile >> word;
}

Hope this helps.

0
source

Source: https://habr.com/ru/post/1739937/


All Articles