मैं जबकि एक फ़ाइल डाउनलोड की जा रही है एक प्रगति बार कंसोल विंडो में प्रदर्शित करना चाहते हैं। मेरा कोड यह है: Download file using libcurl in C/C++।सी ++ libcurl सांत्वना प्रगति बार
libcurl में एक प्रगति बार कैसे करने के लिए?
मैं जबकि एक फ़ाइल डाउनलोड की जा रही है एक प्रगति बार कंसोल विंडो में प्रदर्शित करना चाहते हैं। मेरा कोड यह है: Download file using libcurl in C/C++।सी ++ libcurl सांत्वना प्रगति बार
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;
}
CURLOPT_PROGRESSFUNCTION
समारोह सूचक है कि curl_progress_callback प्रोटोटाइप से मेल खाना चाहिए से में पाया। ऑपरेशन के दौरान अंतराल (लगभग प्रति सेकंड एक बार) अंतराल के दौरान स्थानांतरित होने या नहीं होने पर यह कार्य को libcurl द्वारा आंतरिक समतुल्य के रूप में जाना जाता है। कॉलबैक करने पारित कर दिया अज्ञात/अप्रयुक्त तर्क मान शून्य (जैसे अगर आप केवल डेटा डाउनलोड, अपलोड आकार 0 रहेगा) पर निर्धारित किया जाएगा। रिटर्निंग एक गैर शून्य मान इस कॉलबैक से libcurl स्थानांतरण निरस्त और CURLE_ABORTED_BY_CALLBACK वापस जाने के लिए कारण होगा।
तो:
आप एक समारोह है कि इस
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);
कि सभी
किया जाना चाहिए है किमेरी समस्या यह है कि मैं प्रगति बार के लिए एक काम कोड नहीं कर सकता है। –
@marios अगर मैं 'curl_easy_setopt डाल (कर्ल, CURLOPT_NOPROGRESS, 0);' // कॉलबैक फ़ंक्शन 'curl_easy_setopt (कर्ल, CURLOPT_PROGRESSFUNCTION, progress_func) स्थापित करें;' तो मैं डाउनलोड – user1089679
में असफल हो @ user1089679 यह सुनिश्चित करें कि आपके कॉलबैक में 0 लौट रहा है –
उपयुक्त प्रगति बार की तरह
#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;
}
बहुत बहुत धन्यवाद! मुझे बस गणित शामिल करने की आवश्यकता है और यह ठीक काम करता है। –
@ लेवो, आपको यह पसंद है! – fvu
@fvu हाय यहाँ मैंने आपके द्वारा सुझाए गए तरीके को लागू किया लेकिन मुझे इस फ़ंक्शन progress_func में कोई मूल्य नहीं मिला। – user1089679