Replacing multiple new lines in a file with just one

后端 未结 5 2009
轻奢々
轻奢々 2021-01-22 12:00

This function is supposed to search through a text file for the new line character. When it finds the newline character, it increments the newLine counter, and when

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-22 12:46

    [EDITED] The minimal change is:

    if ( newLine <= 2)
    

    forgive me and forget the previous code.

    a slightly simpler alternative:

    int c;
    int duplicates=0;
    while ((c = fgetc(fileContents)) != EOF)
    {
        if (c == '\n') {
            if (duplicates > 1) continue;
            duplicates++;
        }
        else {
            duplicates=0;
        }
        putchar(c);
    }
    

提交回复
热议问题