C ++の文字列をドキュメントで検索するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/2582415

質問

これがこれまでの私のコードです:

#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;

}

次の言葉、現在この無限のループに行く方法がわかりません...何か推奨されていますか?

また...その言葉がオンになっているという行を印刷するようにどのように伝えますか?

前もって感謝します!

役に立ちましたか?

解決

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

これは仕事をするでしょう。ストリームについてもっと読んでください。

他のヒント

あなたがしたいのは、ファイル内のキーワードの数をカウントすることだけなら、次のとおりです。

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

単語を読みたい場合。
しかし、このように線の数字を印刷してから、次のように機能するはずです。

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;
} 

変化する while(infile.is_open())while(infile). 。その後、最後に冗長なEOFテストを削除できます。

エラーが発生したり、ファイルの終了に到達した場合でも、まだ開いています。 Failbitが設定されているシナリオにいる可能性があります(Getlineは何も返しません)が、EOFは遭遇していないため、ファイルが閉じられないため、ループは終了しません。を使用して operator bool ストリームのこれらすべての問題を回避します。

問題は、ソースコードのこの部分にあります。

getline(cin,word);

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

まず第一に、あなたはからの行を読みたくありません CIN. 。あなたは読みたいです 言葉 から infile. 。したがって、ループ内のコードの最初の行を次のように置き換える必要があります。

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

また、ループの状態を変更する必要があります。を確認する必要はありません infile ここで開いています。あなたはそれをチェックする必要があります ループが始まります。ループについては、 EOF 州に到達したかどうか:

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++;
    }
}

そして、あなたが見ることができるように、今あなたはその奇妙な秒を取り除くことができます もしも ループの中に入れます。
最後のステップは、「読書」テクニックを紹介することです。何も読んでいないときにEOFをテストすることは意味がありません。

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;
}

お役に立てれば。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top