C ++ libcurl控制台进度条 [英] C++ libcurl console progress bar

查看:115
本文介绍了C ++ libcurl控制台进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下载文件时在控制台窗口中显示进度栏。我的代码是: http://stackoverflow.com/questions/1636333 / download-file-using-libcurl-in-cc / 1636827#1636827

I would like a progress bar to appear in the console window while a file is being downloaded. My code is this: http://stackoverflow.com/questions/1636333/download-file-using-libcurl-in-c-c/1636827#1636827.

如何在libcurl中有进度条?

How to have a progress bar in libcurl?

推荐答案

int progress_func(void* ptr, double TotalToDownload, double NowDownloaded, 
                    double TotalToUpload, double NowUploaded)
{
    // ensure that the file to be downloaded is not empty
    // because that would cause a division by zero error later on
    if (TotalToDownload <= 0.0)) {
        return 0;
    }

    // how wide you want the progress meter to be
    int totaldotz=40;
    double fractiondownloaded = NowDownloaded / TotalToDownload;
    // part of the progressmeter that's already "full"
    int dotz = round(fractiondownloaded * totaldotz);

    // create the "meter"
    int ii=0;
    printf("%3.0f%% [",fractiondownloaded*100);
    // part  that's full already
    for ( ; ii < dotz;ii++) {
        printf("=");
    }
    // remaining part (spaces)
    for ( ; ii < totaldotz;ii++) {
        printf(" ");
    }
    // and back to line begin - do not forget the fflush to avoid output buffering problems!
    printf("]\r");
    fflush(stdout);
    // if you don't return 0, the transfer will be aborted - see the documentation
    return 0; 
}

这篇关于C ++ libcurl控制台进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆