【问题标题】:Files in C, cannot copy the elements from a file to anotherC中的文件,不能将元素从一个文件复制到另一个文件
【发布时间】:2021-01-21 18:27:42
【问题描述】:

如果该行是奇数,我正在尝试将元素从一个文件复制到另一个文件; 例如我在第一个文件中:

6
5 6 8
3
6 9 32

输出应该是

5 6 8
6 9 32

但我什么也得不到。

这是我的代码:

int main() {
    FILE *data_input = fopen("data.in", "r");
    FILE *data_out = fopen("data.out", "w");
    char str;
    int nr = 0;

    if (data_input == NULL || data_out == NULL)
        printf("error");

    while ((str = fgetc(data_input)) != EOF) {
        str = fgetc(data_input);

        if (str == "\n")
            nr++;

        if (nr % 2 != 0)
            fputc(str, data_out);
    }

    fclose(data_input);
    fclose(data_out);

    return 0;
}

【问题讨论】:

  • 请使用正确的缩进。这很重要。
  • @EugeneSh。我已经修正了这个问题的缩进。
  • 'char str;' - EOF 是一个整数。
  • while ((str = fgetc(data_input)) != EOF) { str = fgetc(data_input); 什么?
  • while ((str = fgetc(data_input)) != EOF) { str = fgetc(data_input); 哇。我不认为我需要再次证明为什么在while()if() 语句中的填塞 分配是一个BAD IDEA.

标签: c file


【解决方案1】:
  1. 在此之后,您无需再阅读。通过这样做,您将丢弃一半的字符(您读取了 2 个但只写入了一个)。

  2. 您与'\n' 进行比较(与single quotes,而不是双倍)。

  3. 你应该用一个初始化nr,因为第一行可能不是数字0。


int main() {
    FILE *data_input = fopen("data.in", "r");
    FILE *data_out = fopen("data.out", "w");
    char str;
    int nr = 1;

    if (data_input == NULL || data_out == NULL)
        printf("error");

    while ((str = fgetc(data_input)) != EOF) {

        if (str == '\n')
            nr++;
        if (nr % 2 != 0)
            fputc(str, data_out);
    }

    fclose(data_input);
    fclose(data_out);

    return 0;
}

【讨论】:

  • 这将用一行省略源文件的换行符。
【解决方案2】:

首先,你从什么开始数数? 0 还是 1?下面的代码从零开始,所以第一行是偶数。

除了其他问题之外,您使用错误的行发出换行符,这可能会使文件的最后一行错误地为空,因为如果最后一个偶数行为空,则您发出换行符:

int main() {
    FILE *data_input = fopen("data.in", "r");
    FILE *data_out = fopen("data.out", "w");
    char str;
    int nr = 0;

    if (data_input == NULL || data_out == NULL)
        printf("error");

    while ((str = fgetc(data_input)) != EOF) {
        str = fgetc(data_input);

        if (str == "\n")
            nr++;  <--- this should be *AFTER* emitting the output

        if (nr % 2 != 0)
            fputc(str, data_out);
    }

    fclose(data_input);
    fclose(data_out);

    return 0;
}

会更好

int main() {
    FILE *data_input = fopen("data.in", "r");
    FILE *data_out = fopen("data.out", "w");
    int nr = 0;

    if (data_input == NULL || data_out == NULL)
        printf("error");

    // note that this is now easier to read and -
    // importantly - to *debug* along with being
    // **MUCH** less bug-prone
    for (;;) {
        int str = fgetc(data_input);
        if (str == EOF)
            break;

        if (nr % 2 != 0)
            fputc(str, data_out);

        if (str == '\n')
            nr++;
    }

    fclose(data_input);
    fclose(data_out);

    return 0;
}

【讨论】:

    【解决方案3】:

    有多个问题:

    • 如果文件无法打开,请不要退出程序
    • 你在循环中有一个多余的fgets()
    • fgetc() 返回一个int,不要使用char 变量来存储它并测试EOF
    • 给字节命名str 非常混乱
    • 您应该测试并输出字节 before 测试行尾:尾随换行符是行的一部分。
    • 行号通常从 1 开始。第一行的数字 1 是奇数。从您的预期输出中,您需要具有偶数行号的行。

    这是修改后的版本:

    #include <stdio.h>
    
    int main() {
        FILE *data_input = fopen("data.in", "r");
        FILE *data_out = fopen("data.out", "w");
        int c;
        int lineno = 1;  // the first line is line 1
    
        if (data_input == NULL || data_out == NULL) {
            printf("error opening files\n");
            return 1;
        }
    
        while ((c = fgetc(data_input)) != EOF) {
            if (lineno % 2 == 0)  // output lines with an even line number
                fputc(c, data_out);
            if (c == "\n")        // update the line number after the newline
                lineno++;
        }
    
        fclose(data_input);
        fclose(data_out);
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多