我正在尝试在 Qt 4.8 中使用 C++ 和 libcurl 获取网站源代码。我是 Qt 和 C++ 的新手。但是,我收到一条错误消息
错误信息 -
启动 C:*****.exe... 程序意外结束。 C:*****.exe 退出,代码为 -1073741819
#include <curl/include/curl/curl.h>
#include <fstream>
#include <sstream>
#include <iostream>
static size_t data_write(void* buf, size_t size, size_t nmemb, void* userp)
{
if(userp)
{
std::ostream& os = *static_cast<std::ostream*>(userp);
std::streamsize len = size * nmemb;
if(os.write(static_cast<char*>(buf), len))
return len;
}
return 0;
}
CURLcode curl_read(const std::string& url, std::ostream& os, long timeout = 30)
{
CURLcode code(CURLE_FAILED_INIT);
CURL* curl = curl_easy_init();
if(curl)
{
if(CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &data_write))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_FILE, &os))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_URL, url.c_str())))
{
code = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
}
return code;
}
int main()
{
curl_global_init(CURL_GLOBAL_ALL);
if(CURLE_OK == curl_read("http://google.com", std::cout))
{
// Web page successfully written to standard output (console?)
}
curl_global_cleanup();
return 0;
}
上面的代码有什么问题?
PS - 上面的代码不是我的。