私は4つの段落を持つテキストファイル(input.txt)を持っています。これらの段落の文章を次々に別のファイル(output.txt)に書き込む必要がありますライン)。ファイルの文章を次々と出力する
元のファイルの文章は、「。」、「!」で区切られています。と '?'。
私はそれをしましたが、私のコードには問題があります。いくつかの文章は、output.txtと、ファイルに新しい行に書かれていない
マイコード:たとえば
while(1) {
c = fgetc(fp);
if(feof(fp)) {
break;
}
c_next = fgetc(fp);
if(feof(fp)) {
fprintf(fp_output, "%c", c);
break;
} else {
if(c=='.' || c=='?' || c=='!') {
fprintf(fp_output, "%c\n", c);
} else {
if(c=='\n') {
fprintf(fp_output, "%c", c_next);
} else if(c_next=='\n') {
fprintf(fp_output, "%c ", c);
} else {
fprintf(fp_output, "%c%c", c, c_next);
}
}
}
}
、入力ファイルについて:私は次の出力とを得る
This is the first sentence. The second one contains some more words, other words,
more words, etc. The third sentence has; and more like: this, that, those.
This is the second paragraph. And now a question? Only an exclamative
sentence is missing!
This is the third paragraph. Another component - word - would be this.
The final sentence of the paragraph!
This is the last paragraph.
私のコード:
This is the first sentence.
The second one contains some more words, other words, more words, etc. The third sentence has; and more like: this, that, those.
This is the second paragraph.
And now a question?
Only an exclamative sentence is missing!
This is the third paragraph. Another component - word - would be this.The final sentence of the paragraph!
This is the last paragraph.
問題は2番目と6番目の行にあります。各行には最大1つの送信が必要ですence。
任意のアイデアやヒントや解決策を歓迎します。
ありがとう
がc_next'は終止符( '」.'')である'あれば、あなたのコードが何をするか考えてみましょう。このような問題をデバッグする方法は、デバッガを使用して行ごとにコードをステップ実行することです。あなたの 'c'と' c_next'の使用は混乱につながります。私はあなたが 'c_next'なしでそれを行うことができることをお勧めします。 – kaylum
どうすればc_nextなしでもそれを行うことができますか?新しい文章がいつ始まるかを知るにはどうすればいいですか? @kaylumまたは、文字が大文字かどうかをチェックするだけですか? – zeeks
もし完全な停止があれば、それは文が終了したことを伝えます。文章がそこで終わるかどうかを知るために、なぜ完全停止した後に次の文字が必要ですか?次の文字は無関係です。フルストップ自体で十分です。 – kaylum