我有从网页下载汇率的python脚本,我想从那里制作c ++程序,这是我到目前为止:
include iostream
include time.h
include stdio.h
include curl/curl.h
include curl/easy.h
include string
define CURL_STATICLIB
using namespace std;
void dat(string &d){
time_t rawtime;
struct tm * timeinfo;
char datum[80];
time ( &rawtime );
timeinfo=localtime(&rawtime);
strftime(datum,80,"%d%m%y",timeinfo);
d=datum;
}
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
int main()
{
string f;
dat(f);
string l1="http://www.hnb.hr/tecajn/f";
string l2=".dat";
string linkz=l1+f+l2;
cout << linkz;
CURL *curl;
FILE *fp;
CURLcode res;
char *url = linkz;
char outfilename[FILENAME_MAX] = "/home/tomi/data.txt";
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
return 0;
}
当我尝试编译时,它给我这个错误,我发现使用algorythm下载txt,所以我希望它是正确的
答案 0 :(得分:6)
如果您已指出出现错误的行,我就不必将其追踪到:
string linkz=l1+f+l2;
...
char *url = linkz;
您可以使用c_str()
获取指向字符串中const字符的指针。所以这样做:
char const* url = linkz.c_str();
您可以在setopt
来电中使用同一行,也可以将url
设为std::string
。
答案 1 :(得分:6)
char *url = linkz;
应为const char* url = linkz.c_str();
,假设您出于API原因确实需要C风格的字符串。
答案 2 :(得分:3)
问题出在这一行:
char *url = linkz;
“links”是一个std :: string,但“url”是一个char *。尝试使用string的c_str方法来获得所需的内容:
const char * url = links.c_str();