尝试在读取txt文件时跳过“ \ n”时出现问题

我写了一个很小的程序来帮助txt文件格式化,但是当我尝试从输入文件中读取并跳过不需要的'\ n'时,实际上我跳过了'\ n'之后的下一个字符。

我在示例文件中处理的字符是这样的:

abcde
abc

   ab
abcd

我的代码如下:

while (!feof(fp1)) {
    ch = fgetc(fp1);
    if (ch != '\n') {
        printf("%c",ch);
    }
    else {
        ch = fgetc(fp1); // move to the next character
        if (ch == '\n') {
            printf("%c",ch);
        }
    }
}

预期结果是

abcdeabc
  ababcd

但是我实际上得到了

abcdebc
   abbcd

我想问题出在ch = fgetc(fp1); // move to the next character ,但我只是找不到实现此想法的正确方法。

lihonglian26 回答:尝试在读取txt文件时跳过“ \ n”时出现问题

请考虑代码流(下面编号的行):

 1:  while (!feof(fp1)) {
 2:      ch = fgetc(fp1);
 3:      if (ch != '\n') {
 4:          printf("%c",ch);
 5:      }
 6:      else {
 7:          ch = fgetc(fp1); // move to the next character
 8:          if (ch == '\n') {
 9:              printf("%c",ch);
10:          }
11:      }
12:  }

当您得到换行符之后是非换行符时,流程为(从else行开始):6,7,8,10,11,12,1,2

按顺序执行最后一个2可以有效地丢弃您在7上读取的非换行符。


如果您的目的是基本上丢弃单个换行符并将换行符序列(两个或多个)转换为单个换行符(a),则可以使用以下伪代码:

set numNewlines to zero
while not end-file:
    get thisChar
    if numNewlines is one or thisChar is not newline:
        output thisChar
    if thisChar is newline:
        increment numNewlines
    else:
        set numNewlines to zero

这会在一个位置读取字符,从而减少了由于混乱的流程而无意间跳过一个字符的可能性。

它还使用换行符 history 决定要打印的内容。它只在出现 second 的一系列换行符中输出换行符,而忽略第一行和第二行之后的任何行。

这意味着单个换行符将永远不会回显,并且任何两个或更多的组将转换为一个。


一些演示此(b)实际 C代码:

#include <stdio.h>
#include <stdbool.h>

int main(void) {
    // Open file.

    FILE *fp = fopen("testprog.in","r");
    if (fp == NULL) {
        fprintf(stderr,"Cannot open input file\n");
        return 1;
    }

    // Process character by character.

    int numNewlines = 0;
    while (true) {
        // Get next character,stop if none left.

        int ch = fgetc(fp);
        if (ch == EOF) break;

        // Output only second newline in a sequence of newlines,// or any non-nwline.

        if (numNewlines  == 1 || ch != '\n') {
            putchar(ch);
        }

        // Manage sequence information.

        if (ch == '\n') {
            ++numNewlines;
        } else {
            numNewlines = 0;
        }
    }

    // Finish up cleanly.

    fclose(fp);
    return 0;
}

(a)从您的问题尚不清楚,您想如何处理三个或更多换行符的序列,因此我不得不做个假设。


(b)当然,如果您打算学习,则不应使用,因为:

  1. 如果自己尝试并必须解决任何问题,您将学到更多。
  2. 教育机构几乎肯定会通过网络搜索来检查提交的代码,并且您可能会被抄袭。

我只是为了完整性而提供。

本文链接:https://www.f2er.com/3154960.html

大家都在问