C语言中,比较两个文本文件是否相同是一个基础的编程任务,通过合理的设计和优化,可以实现高效准确的比较。在实际应用中,可以根据不同的需求选择合适的方法。

1、逐字节比较

最简单的比较方法。逐个字节读取文件并进行比较。

#include <stdio.h>

int compare_files_byte_by_byte(const char *file1, const char *file2) {
    FILE *fp1 = fopen(file1, "rb");
    FILE *fp2 = fopen(file2, "rb");
    if (!fp1 || !fp2) {
        perror("Failed to open files");
        return -1;
    }

    int ch1, ch2;
    do {
        ch1 = fgetc(fp1);
        ch2 = fgetc(fp2);
        if (ch1 != ch2) {
            fclose(fp1);
            fclose(fp2);
            return 0; // files are not the same
        }
    } while (ch1 != EOF && ch2 != EOF);

    fclose(fp1);
    fclose(fp2);
    return (ch1 == EOF && ch2 == EOF) ? 1 : 0;
}

int main() {
    const char *file1 = "file1.txt";
    const char *file2 = "file2.txt";

    int result = compare_files_byte_by_byte(file1, file2);
    if (result == 1) {
        printf("Files are identical.\n");
    } else if (result == 0) {
        printf("Files are different.\n");
    } else {
        printf("An error occurred.\n");
    }

    return 0;
}

2、逐行比较

将文件逐行读取并比较每一行。

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

#define MAX_LINE 1024

int compare_files_line_by_line(const char *file1, const char *file2) {
    FILE *fp1 = fopen(file1, "r");
    FILE *fp2 = fopen(file2, "r");
    if (!fp1 || !fp2) {
        perror("Failed to open files");
        return -1;
    }

    char line1[MAX_LINE], line2[MAX_LINE];
    while (fgets(line1, MAX_LINE, fp1) && fgets(line2, MAX_LINE, fp2)) {
        if (strcmp(line1, line2) != 0) {
            fclose(fp1);
            fclose(fp2);
            return 0; // files are not the same
        }
    }

    int result = (feof(fp1) && feof(fp2)) ? 1 : 0;
    fclose(fp1);
    fclose(fp2);
    return result;
}

int main() {
    const char *file1 = "file1.txt";
    const char *file2 = "file2.txt";

    int result = compare_files_line_by_line(file1, file2);
    if (result == 1) {
        printf("Files are identical.\n");
    } else if (result == 0) {
        printf("Files are different.\n");
    } else {
        printf("An error occurred.\n");
    }

    return 0;
}

3、通过哈希值比较

使用哈希函数计算文件的哈希值并比较它们是否相同。

#include <stdio.h>
#include <openssl/md5.h>

void compute_md5(FILE *file, unsigned char *result) {
    MD5_CTX md5_ctx;
    unsigned char buffer[1024];
    size_t bytes;

    MD5_Init(&md5_ctx);
    while ((bytes = fread(buffer, 1, sizeof(buffer), file)) != 0) {
        MD5_Update(&md5_ctx, buffer, bytes);
    }
    MD5_Final(result, &md5_ctx);
}

int compare_files_md5(const char *file1, const char *file2) {
    FILE *fp1 = fopen(file1, "rb");
    FILE *fp2 = fopen(file2, "rb");
    if (!fp1 || !fp2) {
        perror("Failed to open files");
        return -1;
    }

    unsigned char hash1[MD5_DIGEST_LENGTH];
    unsigned char hash2[MD5_DIGEST_LENGTH];

    compute_md5(fp1, hash1);
    compute_md5(fp2, hash2);

    fclose(fp1);
    fclose(fp2);

    return memcmp(hash1, hash2, MD5_DIGEST_LENGTH) == 0 ? 1 : 0;
}

int main() {
    const char *file1 = "file1.txt";
    const char *file2 = "file2.txt";

    int result = compare_files_md5(file1, file2);
    if (result == 1) {
        printf("Files are identical.\n");
    } else if (result == 0) {
        printf("Files are different.\n");
    } else {
        printf("An error occurred.\n");
    }

    return 0;
}

推荐文档

相关文档

大家感兴趣的内容

随机列表