11

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++.

How to have a progress bar in libcurl?

Community
  • 1
  • 1

3 Answers3

23

Your meter.

#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; 
}
Lumito
  • 490
  • 6
  • 20
fvu
  • 32,488
  • 6
  • 61
  • 79
  • Thank you very much! I just needed to include math.h and it works fine. –  Oct 28 '09 at 19:15
  • @fvu Hi Here i implemented way which you suggested but i didnt get any value in this function progress_func . – user1089679 May 16 '12 at 03:56
  • Beautiful i have adapted this to C works very very very well! – workdreamer May 13 '15 at 12:01
  • 1
    @workdreamer Glad you liked it, but I'm a bit puzzled, the code above *is* C ;) – fvu May 13 '15 at 12:35
  • 1
    @workdreamer thanks for the edit! If you are using php, also check out [phpclimate](http://climate.thephpleague.com/terminal-objects/progress-bar/) for a simple way to make an even fancier bar :) – fvu May 14 '15 at 12:26
  • The lib is interesting. maybe i will use it later. now i am using your code :) – workdreamer May 14 '15 at 13:33
15

From the curl documentation

CURLOPT_PROGRESSFUNCTION

Function pointer that should match the curl_progress_callback prototype found in . This function gets called by libcurl instead of its internal equivalent with a frequent interval during operation (roughly once per second) no matter if data is being transfered or not. Unknown/unused argument values passed to the callback will be set to zero (like if you only download data, the upload size will remain 0). Returning a non-zero value from this callback will cause libcurl to abort the transfer and return CURLE_ABORTED_BY_CALLBACK.

So:

You provide a function that looks like this

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
}

And some extra options after the existing options

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); 

That's all that needs to be done

fvu
  • 32,488
  • 6
  • 61
  • 79
  • My problem is that I cannot make a working code for the progress bar. –  Oct 28 '09 at 17:34
  • @marios if i `put curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);` // Install the callback function `curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_func);` then i get failed in download – user1089679 May 16 '12 at 03:58
  • 3
    @user1089679 make sure you're returning 0 in your callback – André Morujão Sep 11 '12 at 10:14
2

Like apt progress bar

#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;
}
Pmimo
  • 61
  • 3