程序打印不必要的'ÿ'字符

时间:2013-11-01 20:23:58

标签: c text file-io character printf

我的程序比较两个文本文件,并将文件中的差异放在第三个文本文件中。但是,当我的文件大小比我的文件大两个大小时,'ÿ'字符放在第三个文件的末尾。例如,假设文件一包含“我喜欢泡菜”。文件二包括“我喜欢狗”。那么第三个文件将包含“pickles.ÿ”。有没有办法摆脱这个?为什么会发生这种情况?这是我的计划:

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

int main(int argc, char *argv[])
{

    int ch1, ch2;
    int size1, size2;
    FILE *fh1, *fh2, *diffone=stdout;

    if( argc<3 ) {
        printf("need two file names\n"); return(1);
    }
    if(!(fh1 = fopen(argv[1], "r"))) {
        printf("cannot open %s\n",argv[1]); return(2);
    }
    if(!(fh2 = fopen(argv[2], "r"))) {
        printf("cannot open %s\n",argv[2]); return(3);
    }
    if(argc>3) {
        if(!(diffone = fopen(argv[3], "w+"))) {
            printf("cannot open %s\n",argv[3]); return(4);
        }
    }

    fseek(fh1, 0, SEEK_END);
    size1 = ftell(fh1);//gets size of fh1
    fseek(fh1, 0, SEEK_SET);

    fseek(fh2, 0, SEEK_END);
    size2 = ftell(fh2);//gets size of fh2
    fseek(fh2, 0, SEEK_SET);

    while((!feof(fh1)) || (!feof(fh2)))
    {
        ch1=ch2='-';
        if(!feof(fh1)) ch1 = getc(fh1);
        if(!feof(fh2)) ch2 = getc(fh2);
    if (size2 > size1)
      {
        if(ch1 != ch2 && (!feof(fh1)))
          {
        fprintf(diffone,"%c", ch1);
          }
      }
    else 
      {
        if (ch1 != ch2)
          {
        fprintf(diffone,"%c", ch1);
          }
      }

    }
}

4 个答案:

答案 0 :(得分:3)

feof()仅在true返回EOF(-1)后返回fgetc()

请参阅Why is “while ( !feof (file) )” always wrong?

答案 1 :(得分:0)

您没有对fgetc()进行任何错误检查,并且fh2时您未在size1 > size2检查EOF。

请改为尝试:

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

int main(int argc, char *argv[])
{
    int ch1, ch2;
    int eof1 = 0, eof2 = 0;
    FILE *fh1, *fh2, *diffone=stdout;

    if (argc < 3) {
        printf("need two file names\n");
        return(1);
    }
    if (!(fh1 = fopen(argv[1], "r"))) {
        printf("cannot open %s\n",argv[1]);
        return(2);
    }
    if (!(fh2 = fopen(argv[2], "r"))) {
        printf("cannot open %s\n",argv[2]);
        return(3);
    }
    if (argc > 3) {
        if (!(diffone = fopen(argv[3], "w+"))) {
            printf("cannot open %s\n",argv[3]);
            return(4);
        }
    }

    while(1)
    {
        if (!eof1) {
            ch1 = fgetc(fh1);
            if (ch1 == EOF) {
                if (ferror(fh1)) {
                    printf("cannot read from %s\n",argv[1]);
                    return(5);
                }
                eof1 = 1;
            }
        }
        else {
          ch1 = '-';
        }

        if (!eof2) {
            ch2 = fgetc(fh2);
            if (ch2 == EOF) {
                if (ferror(fh2)) {
                    printf("cannot read from %s\n",argv[2]);
                    return(6);
                }
                eof2 = 1;
            }
        }
        else {
            ch2 = '-';
        }

        if ((eof1) && (eof2))
            break;

        if ((ch1 != ch2) || (eof1 != eof2)) {
            if (fputc(ch1, diffone) == EOF) {
                if (argc > 3) {
                    printf("cannot write to %s\n",argv[3]);
                }
                return(7);
            }
        }
    }
    return 0;
}

答案 2 :(得分:0)

在每个fgetc()之后检查EOF 避免使用feof()。在首次从阅读中返回EOF后,重新调用feof()变为

而不是

while((!feof(fh1)) || (!feof(fh2))) {
    if(!feof(fh1)) ch1 = getc(fh1);
    if(!feof(fh2)) ch2 = getc(fh2);

使用

 while (1) {
   ch1 = fget(fh1);
   ch2 = fget(fh2);
   if (ch1 == EOF) { 
     if (ch2 == EOF) break;
     ch1 = '-';
   }
   if (ch2 == EOF) { 
     ch2 = '-';
   }
   ....
 }

答案 3 :(得分:0)

当您尝试首先从文件中提取然后打印时,会发生此问题 例如 -

ch=fgetc(fp2);
while(ch!=EOF)
{   
    ch = fgetc(fp2);
    fputc(ch, fp1);     
}

INSTEAD DO -

ch=fgetc(fp2);
while(ch!=EOF)
{   
   fputc(ch, fp1);
   ch = fgetc(fp2);
}