c - 一个接一个地打印一个文件的句子

标签 c

我有一个包含 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.

问题在第二行和第六行。每行最多只能有一个句子。

欢迎任何想法或提示或解决方案。

谢谢

最佳答案

以下代码:

  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

关于c - 一个接一个地打印一个文件的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40944947/

相关文章:

c - Solaris-内部编译器错误: cg_inbuf_emit() missed/messed up relocation

c - 使用指针读取结构值

c - 不同的 gdb 寄存器名称

c - C 中的广度优先搜索代码,可能的堆栈溢出

c - 为什么 C 没有逻辑赋值运算符?

c - UNIX C编程输入重定向命令

c++ - 测试 DLL 函数的最佳方法是什么?

c - 在C中定义一个函数以从用户处获取二维矩阵的元素

c - 什么是 LPTHREAD_START_ROUTINE?

c - .c 文件什么时候应该没有关联的 .h 文件?