2016-12-03 3 views
0

私は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。

任意のアイデアやヒントや解決策を歓迎します。

ありがとう

+1

がc_next'は終止符( '」.'')である'あれば、あなたのコードが何をするか考えてみましょう。このような問題をデバッグする方法は、デバッガを使用して行ごとにコードをステップ実行することです。あなたの 'c'と' c_next'の使用は混乱につながります。私はあなたが 'c_next'なしでそれを行うことができることをお勧めします。 – kaylum

+0

どうすればc_nextなしでもそれを行うことができますか?新しい文章がいつ始まるかを知るにはどうすればいいですか? @kaylumまたは、文字が大文字かどうかをチェックするだけですか? – zeeks

+2

もし完全な停止があれば、それは文が終了したことを伝えます。文章がそこで終わるかどうかを知るために、なぜ完全停止した後に次の文字が必要ですか?次の文字は無関係です。フルストップ自体で十分です。 – kaylum

答えて

1

あなたのコードは、時間当たり2文字を読み込みますが、唯一の第一文字をチェックします。したがって、c_nextが "。!?"の場合、単に失敗します。

更新されました。 "if(c == '\ n')"の部分を参照してください。まだコメントはできません。

#include <stdio.h> 

int main(){ 

FILE* fp, *fo; 
int c, flag = -1; 
fp = fopen("input.txt", "r"); 
fo = fopen("output.txt", "w"); 
while(fp != NULL){ 
    c = fgetc(fp); 
    if(feof(fp)) break; 

    if(c=='\n'){ 
    fprintf(fo, " "); 
    continue; 
    } 

    if(flag != '.' && flag != '!' && flag != '?'){ 
    fprintf(fo, "%c", c); 
    } 
    else{ 
    fprintf(fo, "\n"); 
    if(c != ' '){ 
     fprintf(fo, "%c", c); 
    } 
    } 
    flag = c; 
} 
fclose(fp); 
fclose(fo); 

return 0; 
} 
+0

これはうまくいきますが、入力ファイル内の文が2行に分割され、2行がoutput.txtファイルで一緒に結合されると、最初の行末2つの言葉。 Like: これは第2段落です。そして今、質問?感嘆符 文だけがありません! 参加後: 感嘆符だけがありません! (exclamintentenceの間にスペースはない) – zeeks

2

次のコード

  1. のみ軽く
  2. をテストされているクリーン
  3. 改行配列を無視
  4. 空白で区切られていても、文のマーカーの複数のエンドを無視をコンパイル
  5. は、文の前の空白を無視します。

と今、コード

#include <stdio.h> // fopen(), fclose(), fgetc(), putchar() 
#include <ctype.h> // isalpha() 
#include <stdlib.h> // exit(), EXIT_FAILURE 

#define PERIOD (',') 
#define QUESTION_MARK ('?') 
#define EXCLAMATION_MARK ('!') 

int main(int argc, char *argv[]) 
{ 
    if(2 > argc) 
    { 
     fprintf(stderr, "USAGE: %s <inputFileName>\n", argv[0]); 
     exit(EXIT_FAILURE); 
    } 

    // implied else, command line parameter exists 

    FILE *fp = NULL; 
    if(NULL == (fp = fopen(argv[1], "r"))) 
    { 
     perror("fopen failed"); 
     exit(EXIT_FAILURE); 
    } 

    // implied else, fopen successful 

    int inSentence = 0; 
    int ch; 
    while(EOF != (ch = fgetc(fp))) 
    { 
     if(inSentence) 
     { 
      putchar(ch); 
      if(PERIOD == ch || EXCLAMATION_MARK == ch || QUESTION_MARK == ch) 
      { 
       inSentence = 0; 
       putchar('\n'); 
      } 
     } 

     else // if(!inSentence) 
     { 
      if(isalpha(ch)) 
      { // then not white space nor more punctuation 
       inSentence = 1; 
       putchar(ch); 
      } 
     } 
    } // end while 

    // cleanup 
    if(inSentence) 
    { 
     putchar('\n'); 
    } 

    fclose(fp); 
} // end function: main 
関連する問題