【问题标题】:C file handling: Text doesn't append at the end of the fileC 文件处理:文本不附加在文件末尾
【发布时间】:2012-07-28 05:51:34
【问题描述】:

程序应该查看file1 的每一行,然后查看file2 中是否存在完全相同的行。如果是,则将该行复制到一个名为 output 的新文件中。

说,以下是文件的内容(可能是句子,但为了简单起见,我放了数字。)-

  file1              file2
    1                 2
    2                 4
    3                 15
    4                 6
    5                 11
    6                 8
    7
    8
    9

那么output文件应该看起来像-

 (Expected) output
              2
              4
              6
              8

在 shell 内部,我可以看到 printf 按预期打印 output,但 fprintf 颠倒了顺序,我不明白为什么?打印到输出文件的输出是-

 output
    8
    6
    4
    2

这是代码 -

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    FILE *file1, *file2, *output;
    int c;

    /*Creating buffers where data will be stored for comparison*/   
    char buffer1[999], buffer2[999];

    /*Settig the buffer characters array to 0*/
    memset(buffer1, 0, sizeof(buffer1));
    memset(buffer2, 0, sizeof(buffer2));

    /*Open output file in "w" so that it clears the file*/
    output = fopen("output", "w");
    /*Open file1 and then save the line inside buffer1 (within while loop)*/
    file1 = fopen("file1", "r");
    /*Read each character in file until End Of Line*/
    while((c = getc(file1)) != EOF)
    {
        int i = 0;
        /*Save each new line of file1 in buffer1 for comparison*/
        while(c != '\n')
        {
            buffer1[i++] = c;
            c = getc(file1);
        }

        /*Open file2 and then save it's line in buffer2 (withing while loop)*/      
        file2 = fopen("file2", "r");
        int ch;

        while((ch = getc(file2)) != EOF)
        {
            i = 0;
            while(ch != '\n')
            {
                buffer2[i++] = ch;
                ch = getc(file2);
            }

            /*Compare lines of file1 against each line of file2*/
            if(strcmp(buffer1,buffer2) == 0)
            {
                /*Save similar lines in a file named output*/
                output = fopen("output", "a");
                fprintf(output,"%s\n", buffer2);
                printf("%s\n", buffer2);
                break;
            }
            /*Reset the buffer*/
            memset(buffer2, 0, sizeof(buffer2));
        }

        memset(buffer1, 0, sizeof(buffer1));
    }

    /*Close the output file if there were any comparison made i.e. if file was opened*/
    if(output != NULL)
    {
        fclose(output);
    }

    /*Close other files*/
    fclose(file1);
    fclose(file2);

    return 0;
}

【问题讨论】:

  • PS:我是初学者,仍然不确定我是否正确编写代码,请善待:) 谢谢。

标签: c file-handling


【解决方案1】:

您正在打开output在每个差异处最后只关闭一次这是错误的,可能会导致您的问题。尝试打开一次output,可能在循环之前。如果您没有发现任何差异,您可以将其删除,以避免出现空文件。

【讨论】:

  • 就是这样,非常感谢。我自己无法弄清楚,对我来说并不是很明显,我每次都需要关闭它。顺便说一句,现在看,我觉得这不是最优雅的方式(打开/关闭写每一行。)你认为有没有更好的方法来处理它?
  • @GuravButola 如果你打开和关闭一次就更好了,没有理由每次都打开之前写:) 所以一开始就打开,想写什么就写,最后关闭。
  • 哦,好吧!这似乎有效,并给出了输出,但在我运行它时给出了一些错误。 pastebin.com/s8SLD8PM
  • @GauravButola 似乎与文件无关,一些指针似乎已损坏
  • 知道为什么这在 Windows 下似乎根本不起作用吗?它直接崩溃。我没有使用任何 Linux 特定的 AFAICT;还是我应该问一个不同的问题?
猜你喜欢
  • 2017-11-08
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 2016-11-10
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 2014-02-24
相关资源
最近更新 更多