比较c中的两个txt文件

时间:2016-06-25 20:07:21

标签: c fgets strcmp

我正在尝试比较2个文本文件并打印它们不同的第一行,但我在fgets()命令中使用了500的缓冲区,我想我在浪费空间。 如果我不知道线的长度,我怎么能制作相同的节目呢?

这是我的代码:

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

int main(int argc, char const *argv[])
{
    FILE *fp1, *fp2;

    int nLine = 1;
    char l1[500], l2[500];

    system("clear");

    if (argc < 3)
    {
        printf("Usage: %s <file1.txt> <file2.txt>\n",argv[0]);
        exit(1);
    }

    if ((fp1 = fopen(argv[1],"r")) == NULL){
        printf("Can't open file: %s\n", argv[1]);
        exit(1);
    }

    if ((fp2 = fopen(argv[2],"r")) == NULL){
        printf("Can't open file: %s\n", argv[2]);
        exit(1);
    }

    fgets(l1,500,fp1);
    fgets(l2,500,fp2);

    while ((l1 != 0) && (l2 != 0)){
        if(strcmp(l1,l2) != 0){
            printf("Line number: %d\n", nLine);
            printf("%s", l1);
            printf("%s\n", l2);
            exit(1);
        } else {
            fgets(l1,500,fp1);
            fgets(l2,500,fp2);
            nLine++;    
        }   
    }

    return 0;
}

1 个答案:

答案 0 :(得分:2)

如果您不想“浪费空间”,请记住数据位于文件内存中。一次读1个字符。当您发现差异时,只需搜索到上一个换行符的位置并报告以下行。

long index = 0;
long index_lf = 0;
int c1,c2;

// read until a difference or done
while ((c1 = fgetc(fp1)) == (c2 = fgetc(fp2)) && (c1 != EOF)) {
  index++;
  if (c1 == '\n') index_lf = index;
}
if (c1 == c2) {
  puts("same");
} else {
  puts("differ");
  fseek(fp1, index_lf, SEEK_SET);
  fseek(fp2, index_lf, SEEK_SET);
  // read and print each file until a following \n or EOF occurs.
  // TBD code for OP
}

[编辑]一些改进以应对各种问题:最后一个字节不匹配,以不同模式打开的文件,错误处理等。

long offset1 = ftell(fp1);;
long offset2 = ftell(fp2);;
int c1,c2;

// read until a difference or done
while ((c1 = fgetc(fp1)) == (c2 = fgetc(fp2)) && (c1 != EOF)) {
  if (c1 == '\n') {
    offset1 = ftell(fp1);
    offset2 = ftell(fp2);
  }
}

if (offset1 == -1 || offset2 == -1 || ferror(fp1) || ferror(fp2)) {
  puts("problem");
} else if (c1 == c2) {
  puts("same");
} else {
  puts("differ");
  fseek(fp1, offset1, SEEK_SET);
  fseek(fp2, offset2, SEEK_SET);
  // read and print each file until a following \n or EOF occurs.
  // TBD code for OP
}
相关问题