我有一个包含 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.
问题在第二行和第六行。每行最多只能有一个句子。
欢迎任何想法或提示或解决方案。
谢谢
最佳答案
以下代码:
- 仅经过轻微测试
- 干净地编译
- 忽略多个句尾标记,即使用空格分隔也是如此
- 忽略换行序列
- 忽略句子前的空白
现在,代码
#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/