首页 > 解决方案 > C 比较两个二进制数据的问题

问题描述

我正在编写一个程序,它获取一个包含两个图片文件的文件夹的路径和一个包含模拟器病毒二进制数据的文件的路径。这个最终项目的目的是扫描文件夹中的文件并判断是否有病毒。我成功打开了所有文件,但我的比较不起作用。请有人帮我修复此代码,因为我不知道为什么会发生这种情况,这是我要获得成绩的最后一个项目。当我使用memcmp函数时,问题就出在这条线上。

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

int print_menu(char *argv[]); // this is a function that print to the user the menu and it's not appears in this question.
void long_scan(char *info[]);

#define STR_LEN 100

int main(int argc, char *argv[])
{
    int option = 0;
    if (argc == 3)
    {
        option = print_menu(argv);
        if (option == 0)
        {
            long_scan(argv);
        }
    }
    else
    {
        printf("The program didn't got the number of paramter's that she expect to get\n");
    }
    getchar();
    return 0;
}
void long_scan(char *info[])
{

    DIR *file = 0; // the folder
    FILE *file_to_check = 0; // the files to check in this case the pictures
    FILE *virus; // the virus file
    struct dirent *files = 0;
    char temp[STR_LEN] = { 0 }; // using to play with the strings to open the files
    int len = 0, result = 0, c = 0, virus_len = 0, i = 0;

    char file_p[STR_LEN] = { 0 }; 
    char virus_path[STR_LEN] = { 0 };

    strcpy(file_p, info[1]); // the path of the folder
    strcpy(virus_path, info[2]); // the path to the virus

    file = opendir(file_p); // opening the folder

    strcat(file_p, "\\\\"); add "\\" to the path so I can get the files in the folder later on
    virus = fopen(virus_path, "rb"); // opening the virus

    fseek(virus, 0, SEEK_END);
    virus_len = ftell(virus); // get the virus len = 494
    fseek(virus, 0, SEEK_SET);
    char* virus_holder = (char *)malloc(sizeof(char) * virus_len); // create variable for the virus
    fread(virus_holder, sizeof(char), virus_len, virus); // get the virus to the variable (his len is 61 dont know why)

    if (file == NULL) // check if the file exist
    {
        printf("Error. file does not exists\n");
        exit(0);
    }
    while ((files = readdir(file)) != NULL) // get one file every time
    {
        if (strcmp(files->d_name, "..") && strcmp(files->d_name, "."))
        {
            strcpy(temp, file_p); // copy the original path for later
            strcat(file_p, files->d_name); // get the path to the file
            file_to_check = fopen(file_p, "rb"); // open the file (picture)
            fseek(file_to_check, 0, SEEK_END);
            len = ftell(file_to_check); // get the file len
            fseek(file_to_check, 0, SEEK_SET);
            char* buffer = (char *)malloc(sizeof(char) * len); // create buffer to hold the file binary data
            fread(buffer, sizeof(char), len, file_to_check);
            fclose(file_to_check);
            /*******/
            result = memcmp(virus_holder, buffer, virus_len); // here the problem his. for some resone th compare does not work for me. and one of the file his holding the virus for 100%
            /*******/
            if (result == 0)
            {
                printf("%s is holding the virus\n", files->d_name);
            }
            strcpy(file_p, temp); // get the original path
            free(buffer);
        }
    }
    free(virus_holder);
}

标签: c

解决方案


推荐阅读