C++ C++;libcurl控制台进度条

C++ C++;libcurl控制台进度条,c++,console,progress-bar,libcurl,C++,Console,Progress Bar,Libcurl,我想在下载文件时在控制台窗口中显示一个进度条。我的代码是: 如何在libcurl中设置进度条?来自 CURLOPT_函数 函数指针,该指针应与 找到curl\u progress\u回调原型 在里面此函数将 由libcurl而不是其 具有频繁的内部等价项 运行期间的间隔(大致为 每秒一次)无论数据是否 是否被转移。 传递了未知/未使用的参数值 将回调的值设置为零 (例如,如果只下载数据 上载大小将保持为0)。返回 来自此回调的非零值 将导致libcurl中止 调回 CURLE_被_回调中止 因此

我想在下载文件时在控制台窗口中显示一个进度条。我的代码是:

如何在libcurl中设置进度条?

来自

CURLOPT_函数

函数指针,该指针应与 找到curl\u progress\u回调原型 在里面此函数将 由libcurl而不是其 具有频繁的内部等价项 运行期间的间隔(大致为 每秒一次)无论数据是否 是否被转移。 传递了未知/未使用的参数值 将回调的值设置为零 (例如,如果只下载数据 上载大小将保持为0)。返回 来自此回调的非零值 将导致libcurl中止 调回 CURLE_被_回调中止

因此:

您提供的函数如下所示

int progress_func(void* ptr, double TotalToDownload, double NowDownloaded, double TotalToUpload, double NowUploaded)
{
    // It's here you will write the code for the progress message or bar
}
以及在现有选项之后的一些额外选项

curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);  // already there
// Internal CURL progressmeter must be disabled if we provide our own callback
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
// Install the callback function
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_func); 
这就是你需要做的全部工作

#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("]\r");
    fflush(stdout);
    // if you don't return 0, the transfer will be aborted - see the documentation
    return 0; 
}
#包括
int progress_func(void*ptr,双倍TotalToDownload,双倍NowDownload,
双倍总上传,双倍现在上传)
{
//确保要下载的文件不是空的
//因为这会导致一个被零除的错误
如果(TotalToDownload与apt进度条类似

#include <iostream>
#include <fstream>
#include <include/curl/curl.h>//Or #include <curl/curl.h>
#include <windows.h>
#include <math.h>

using namespace std;

int nb_bar;
double last_progress, progress_bar_adv;

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

int progress_bar (void *bar, double t, double d)
{
    if(last_progress != round(d/t*100))
    {
      nb_bar = 25;
      progress_bar_adv = round(d/t*nb_bar);

      cout<<"\r ";
      SetConsoleTextAttribute(hConsole, 160);
      cout<<" Progress : [ ";

      if(round(d/t*100) < 10)
      { cout<<"0"<<round(d/t*100)<<" %]"; }
      else
      { cout<<round(d/t*100)<<" %] "; }

      SetConsoleTextAttribute(hConsole, 15);
      cout<<" [";
      SetConsoleTextAttribute(hConsole, 10);
      for(int i = 0 ; i <= progress_bar_adv ; i++)
      { cout<<"#"; }
      SetConsoleTextAttribute(hConsole, 15);
      for(int i = 0 ; i < nb_bar - progress_bar_adv; i++)
      { cout<<"."; }

      cout<<"]";
      last_progress = round(d/t*100);
    }
  return 0;
}


int main()
{
  CURL *curl_download;
  FILE *fp;
  CURLcode res;
  string url = "http://www.gecif.net/articles/mathematiques/pi/pi_1_million.txt", output_file = "pi.txt";

  curl_download = curl_easy_init();

  if (curl_download)
  {
      //SetConsoleTextAttribute(hConsole, 11);
      fp = fopen(output_file.c_str(),"wb");

      curl_easy_setopt(curl_download, CURLOPT_URL, url.c_str());
      curl_easy_setopt(curl_download, CURLOPT_WRITEFUNCTION, NULL);
      curl_easy_setopt(curl_download, CURLOPT_WRITEDATA, fp);
      curl_easy_setopt(curl_download, CURLOPT_NOPROGRESS, FALSE);
      //progress_bar : the fonction for the progress bar
      curl_easy_setopt(curl_download, CURLOPT_PROGRESSFUNCTION, progress_bar);

      //Text color :   SetConsoleTextAttribute(hConsole, nb_color);
      SetConsoleTextAttribute(hConsole, 11);
      cout<<" Start download"<<endl<<endl;

      res = curl_easy_perform(curl_download);

      fclose(fp);
      if(res == CURLE_OK)
      {
        SetConsoleTextAttribute(hConsole, 10);
        cout<<endl<<endl<<" The file was download with succes"<<endl;
      }
      else
      {
        SetConsoleTextAttribute(hConsole, 4);
        cout<<endl<<endl<<" Error"<<endl;
      }
      curl_easy_cleanup(curl_download);
  }
  SetConsoleTextAttribute(hConsole, 11);
  return 0;
}
#包括
#包括
#包括//或#包括
#包括
#包括
使用名称空间std;
国际酒吧;
双倍的最后进度,进度条;
HANDLE hConsole=GetStdHandle(标准输出句柄);
内部进度条(空*条,双t,双d)
{
如果(最后一次进度!=第二轮(d/t*100))
{
nb_bar=25;
进度条=圆形(d/t*nb条);

coutMy的问题是我无法为进度条生成有效代码。@marios如果我
put curl\u easy\u setopt(curl,CURLOPT\u NOPROGRESS,0);
//安装回调函数
curl\u easy\u setopt(curl,CURLOPT\u PROGRESSFUNCTION,progress\u func)
然后我就失败了download@user1089679确保您在回拨中返回0非常感谢!我只需要包含math.h,它工作得很好。@fvu Hi这里我实现了您建议的方法,但我在这个函数中没有得到任何值progress\u func。很漂亮,我已经将它改编为C,效果非常好!@work铰刀很高兴你喜欢它,但我有点困惑,上面的代码是C;)@workdreamer谢谢你的编辑!如果你使用的是php,也可以查看一个简单的方法来制作一个更棒的条:)