मैं अपने सी ++ प्रोग्राम में libcurl के साथ एक फ़ाइल डाउनलोड कर रहा हूं। मैं कैसे पता लगा सकता हूं कि अनुरोध 404 है, और फ़ाइल लिखना नहीं है? कोड है:libcurl 404 पहचान
void GameImage::DownloadImage(string file_name) {
string game_name;
game_name = file_name.substr(file_name.find_last_of("/")+1);
CURL *curl;
FILE *fp;
CURLcode res;
string url = "http://site/"+game_name+".png";
string outfilename = file_name+".png";
cout<<"INFO; attempting to download "<<url<<"..."<<endl;
curl = curl_easy_init();
if (curl) {
cout<<"INFO; downloading "<<url<<"..."<<endl;
fp = fopen(outfilename.c_str(), "wb");
cout<<"INFO; trying to open "<<outfilename<<" for file output"<<endl;
if (fp != NULL) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, GameImage::WriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
res = curl_easy_perform(curl);
long http_code = 0;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
curl_easy_cleanup(curl);
fclose(fp);
}
else {
cout<<"GameImage::DownloadImage; Couldn't open output file"<<endl;
}
}
}
size_t GameImage::WriteData(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
स्थानांतरण के बाद 404 प्रतिक्रिया हटा दी जा सकती है, लेकिन यह प्रतिक्रिया भी सहेजना अच्छा नहीं होगा।
मुझे अभी भी एक फ़ाइल मिलती है, लेकिन अब यह खाली है। मैंने curl_easy_setopt जोड़ा (कर्ल, CURLOPT_FAILONERROR, सच); मुझे कोई परवाह नहीं है कि एक त्रुटि हुई, मैं नहीं चाहता कि फ़ाइल पीछे छोड़ी जाए। – zombor
क्या आप अपने प्रश्न के साथ अपना प्रश्न अपडेट कर सकते हैं? – Alex
हो गया। मुझे लगता है कि यह प्रारंभिक fp = fopen (outfilename.c_str(), "wb") की वजह से है; जो फाइल हैंडल बनाता है। – zombor