简体   繁体   English

这不是一个字节一个字节的比较:为什么

[英]This is not a byte by byte comparison: WHY

I would like to take two files, compare them byte-by-byte, and test it's performance, 我想获取两个文件,逐字节比较它们,并测试其性能,

So far, this is what my code looks like: 到目前为止,这是我的代码:

#include<stdio.h>
#include <time.h>

int main()
{
    FILE *fp1, *fp2;
    int ch1, ch2;
    char fname1[40], fname2[40] ;

    printf("Enter name of first file :") ;
    gets(fname1);

    printf("Enter name of second file:");
    gets(fname2);

    clock();    

    fp1 = fopen( fname1,  "r" );
    fp2 = fopen( fname2,  "r" ) ;

    if ( fp1 == NULL )
    {
        printf("Cannot open %s for reading\n", fname1 );
        exit(1);
    }
    else if (fp2 == NULL)
    {
        printf("Cannot open %s for reading\n", fname2 );
        exit(1);
    }
    else
    {
        ch1  =  getc( fp1 ) ;
        ch2  =  getc( fp2 ) ;

        while( (ch1!=EOF) && (ch2!=EOF) && (ch1 == ch2))
        {
            ch1 = getc(fp1);
            ch2 = getc(fp2);
        }

        if (ch1 == ch2)
            printf("Files are identical\n");
        else if (ch1 != ch2)
            printf("Files are Not identical\n");

        fclose ( fp1 );
        fclose ( fp2 );
    }

    printf("That took %d seconds.\n", clock() / CLOCKS_PER_SEC);

    return 0;
}

I was told: "==" is NOT a byte-by-byte comparison, how can I implement this so that I can XOR bitwise operations? 有人告诉我:“ ==”不是逐字节比较,如何实现此功能,以便可以对按位运算进行XOR?

Also, is there a way to check how much duplication is in a single file itself? 另外,有没有办法检查单个文件本身中有多少重复?

Thanks for your help in advance! 谢谢您的帮助!

In example 2, you aren't checking for end of file. 在示例2中,您无需检查文件结尾。 That's why it never stops. 这就是为什么它永远不会停止的原因。

You are comparing byte by byte. 您正在逐字节比较。 I think both versions will be fine once you fix the second version. 我认为,一旦您修复了第二个版本,两个版本都可以。

Another couple things you might consider: 您可能会考虑的另外几件事:

  • If this may run on a Windows machine, use "rb" instead of "r" so that Windows won't convert line ending characters. 如果此程序可以在Windows计算机上运行,​​请使用“ rb”而不是“ r”,以便Windows不会转换行尾字符。
  • You might want to add a ferror() test as well as the EOF. 您可能要添加ferror()测试以及EOF。 You won't usually need it, but I normally include it just in case. 您通常不需要它,但是为了防万一,我通常将其包括在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM