编辑代码

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

#define BUFFER_SIZE 4
#define PNG_MAGIC_NUMBER "CDTC"

bool checkFileMagicNumber(const char *filePath, const char *magic_number) {
    FILE *file = fopen(filePath, "rb");
    if (file == NULL) {
        perror("Failed to open file");
        printf("Failed to open file\n");
        return false;
    }

    uint8_t buffer[BUFFER_SIZE];
    size_t bytesRead = fread(buffer, 1, BUFFER_SIZE, file);
    if (bytesRead != BUFFER_SIZE) {
        fclose(file);
        return false;
    }

    bool isMatch = (memcmp(buffer, magic_number, BUFFER_SIZE) == 0);
    fclose(file);
    return isMatch;
}

int main() {
    const char *filePath = "F:/资料/定制/20240530_2031.cdtCfg";
    const char *magicNumber = "CDTC";
    if (checkFileMagicNumber(filePath, magicNumber)) {
        printf("The file '%s' has the correct magic number for a PNG file.\n", filePath);
    } else {
        printf("The file '%s' does not have the correct magic number for a PNG file.\n", filePath);
    }
    return 0;
}