4

これまでの私のコードは次のとおりです。

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

}

次の単語に移動する方法がわかりません。現在、この無限ループが発生しています...何か推奨事項はありますか?

また...その単語があった行を印刷するにはどうすればよいですか?

前もって感謝します!

4

4 に答える 4

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

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

于 2010-04-06T03:05:19.547 に答える
2

ファイル内のキーワードの数を数えることだけが必要な場合は、次のようにします。

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;
} 
于 2010-04-07T15:54:41.183 に答える
0

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

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

お役に立てれば。

于 2010-04-06T08:23:22.170 に答える
-1

に変更while(infile.is_open())while(infile)ます。次に、最後に冗長な eof テストを削除できます。

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

于 2010-04-06T03:08:13.063 に答える