c语言版统计文件比对率的demo 没技术含量 存档用

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

int main(int argc, char *argv[])
{
    printf("%s running\n", argv[0]);
    if(argc != 3)
    {
        printf("usage:%s <file1> <file2>\n", argv[0]);
        return 1;
    }

    char *file0 = argv[1];
    char *file1 = argv[2];
    FILE *fp0, *fp1;

    fp0 = fopen(file0, "r");
    if(fp0 == NULL)
    {
        printf("can't open %s\n", file0);
        return 1;
    }

    fp1 = fopen(file1, "r");
    if(fp1 == NULL)
    {
        printf("can't open %s\n", file1);
        return 1;
    }
    //////////////////////////////////////////

    fseek(fp0, 0L, SEEK_END);
    int fp0_size = ftell(fp0);
    fseek(fp0, 0L, SEEK_SET);
    fseek(fp1, 0L, SEEK_END);
    int fp1_size = ftell(fp1);
    fseek(fp1, 0L, SEEK_SET);

    if(fp0_size != fp1_size)
    {
        printf("file1 size != file2 size\n");
        return 1;
    }
    //////////////////////////////////////////

    uint8_t *fp0_buf = (uint8_t *)malloc(fp0_size);
    uint8_t *fp1_buf = (uint8_t *)malloc(fp1_size);

    fread(fp0_buf, fp0_size, 1, fp0);
    fclose(fp0);

    fread(fp1_buf, fp1_size, 1, fp1);
    fclose(fp1);

    int notmatch = 0;
    int grater1  = 0;
    int grater2  = 0;
    int less1    = 0;
    int less2    = 0;
    int other    = 0;
    for(int i = 0; i < fp0_size; i++)
    {
        if(fp1_buf[i] != fp0_buf[i])
        {
            notmatch++;
            if(fp1_buf[i] == fp0_buf[i] + 1)
            {
                grater1++;
            } else if(fp1_buf[i] == fp0_buf[i] + 2)
            {
                grater2++;
            } else if(fp1_buf[i] == fp0_buf[i] - 1)
            {
                less1++;
            } else if(fp1_buf[i] == fp0_buf[i] - 2)
            {
                less2++;
            } else
            {
                other++;
            }
        }
    }

    printf("notmatch =%f%%\n", (float)notmatch * 100 / fp0_size);
    printf("grater1 =%f%%\n", (float)grater1 * 100 / fp0_size);
    printf("grater2 =%f%%\n", (float)grater2 * 100 / fp0_size);
    printf("less1 =%f%%\n", (float)less1 * 100 / fp0_size);
    printf("less2 =%f%%\n", (float)less2 * 100 / fp0_size);
    printf("other =%f%%\n", (float)other * 100 / fp0_size);

    return 0;
}
posted @ 2022-05-10 21:33  园友1683564  阅读(22)  评论(0编辑  收藏  举报