C++ libcurl console progress bar

后端 未结 3 1049
攒了一身酷
攒了一身酷 2020-12-14 23:32

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 p

3条回答
  •  情话喂你
    2020-12-14 23:50

    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

提交回复
热议问题