-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcURL.c
52 lines (38 loc) · 1.02 KB
/
cURL.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <curl/curl.h>
#include "cURL.h"
const char *retrieveXML(const char *URL) {
CURL *curl;
CURLcode curl_res;
long http_code;
double c_length;
const char *fileName = "data.xml";
FILE *dataFile;
dataFile=fopen(fileName, "w");
if(dataFile==NULL) {
printf("\n\n--------------\nERROR opening file roads.xml\n--------------\n");
exit(2);
}
//init curl session
curl = curl_easy_init();
//set url to download
curl_easy_setopt(curl, CURLOPT_URL, URL);
//set file handler to write
curl_easy_setopt(curl, CURLOPT_WRITEDATA, dataFile);
//download the file
curl_res = curl_easy_perform(curl);
if(curl_res!=0) {
printf("\n\n--------------\nERROR in dowloading file\n--------------\n");
fclose(dataFile);
curl_easy_cleanup(curl);
exit(2);
}
//END: close all files and sessions
fclose(dataFile);
curl_easy_cleanup(curl);
return fileName;
}