我试图找到一种方法来下载文件并让程序等待下载完成然后继续下一个任务,在这种情况下将检查文件是否存在
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <fstream>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
using namespace std;
int main(void) {
CURL *curl;
FILE *fp;
CURLcode res;
char *url = "http://199.91.153.180/y1plsynnb11g/e9g9mcd3fi5txov/UHC.jar";
char outfilename[FILENAME_MAX] = "UHC.jar";
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, "http://199.91.153.180/y1plsynnb11g/e9g9mcd3fi5txov/UHC.jar");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
fclose(fp);
}
ofstream launch;
launch.open("UHC.jar");
if (launch.is_open()){
cout<< "File Downloaded";
}
else {
cout << "Download Failed";
}
}