C++ libcurl 控制台进度条

2022-01-11 00:00:00 console progress-bar libcurl c++

我希望在下载文件时在控制台窗口中显示一个进度条.我的代码是这样的:在 C/C++ 中使用 libcurl 下载文件.

I would like a progress bar to appear in the console window while a file is being downloaded. My code is this: Download file using libcurl in C/C++.

如何在libcurl中有进度条?

How to have a progress bar in libcurl?

推荐答案

你的仪表.

#include <math.h>

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 = (int) 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("]");
    fflush(stdout);
    // if you don't return 0, the transfer will be aborted - see the documentation
    return 0; 
}

相关文章