使用md5sum命令计算文件md5

使用md5sum命令计算文件md5

#include <iostream>

using namespace std;

int get_file_md5(const char *file, string &md5_str)
{
    
    

    string path = string(file);

    string cmd = "md5sum " + path;

    FILE *fp = nullptr;

    char buffer[1024] = {
    
    0};

    fp = popen(cmd.c_str(), "r");

    if(fp == nullptr)
    {
    
    
        return -1;
    }

    fgets(buffer, sizeof(buffer), fp);

    pclose(fp);

    string s = string(buffer);
    md5_str = s.substr(0, s.find_first_of(" "));
    return 0;
}

int main()
{
    
    
    string md5_str;
    char file[] = "./md5file";
    int ret = get_file_md5(file, md5_str);

    if(ret == 0)
    {
    
    
        printf("%s\n", md5_str.c_str());
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/bigfanofloT/article/details/108638683
今日推荐