おそらく簡単な答えであり、私の質問を理解しやすいことを願っています。多くの行といくつかの段落を含むテキスト ドキュメントがあります。
手動で行うことなく、すべての行を 1 行にマージする方法はありますか? 私がこれを尋ねている理由は、1 つの段落しかないドキュメント内の特定の単語を検索しているとしましょう (段落に 10 行あるとします) その単語がその 1 つの段落にある場合、paragraph1 が受け入れられると仮定します。しかし、私のプログラムが何か奇妙なことをしている代わりに、次のような出力が得られます。
paragraph1:accepted
paragraph2:accepted
paragraph3:accepted
paragraph4:accepted
paragraph5:accepted
paragraph6:accepted
paragraph7:accepted
paragraph8:accepted
paragraph9:accepted
paragraph10:accepted
これは私のドキュメントを読み取るコードです
void processParagraph(std::string ¶graph, size_t paragraphNumber)
{
ifstream input;
input.open("data.txt"); //opens the text file with the documents
if (input.fail()) //if the file doesn't open
{
cout << "file not found" << endl;
return;
}
if (isMyWordThere(paragraph)) //if the word im looking its there
cout << "paragraph " << paragraphNumber << ": accepted" << endl;
else // if the word its not there
cout << "paragraph " << paragraphNumber << ": not accepted" << endl;
paragraph.clear(); // reset the paragraph to handle the next one.
std::string line;
std::string paragraph;
size_t paragraphNumber = 0;
while ( getline(input, line) ) // read a LINE
{
if ( !line.empty() ) // paragraph not finished
{
paragraph.append("\n").append(line);
}
else // paragraph finished, because we found an empty line
{
++paragraphNumber;
processParagraph(paragraph, paragraphNumber);
}
}
if ( !paragraph.empty() )
{
processParagraph(paragraph, paragraphNumber);
}
}
すべての行を 1 つにマージする方法、または行をカウントしないようにコードを変更する方法はありますか?
入力ファイルは次のようになります:(「hello」という単語を探しています)
hello world
hello everyone
hello All
出力は次のようになります
paragraph 1: accepted
しかし、私が得ているのは
paragraph 1: accepted
paragraph 2: accepted
paragraph 3: accepted