5

我正在从 perl 扩展到 C,我正在尝试使用 curl 的库来简单地从远程 url 保存文件,但我很难找到一个很好的例子来工作。

另外,我不确定是否应该使用 curl_easy_recv 或 curl_easy_perform

4

1 回答 1

6

我发现这个资源对开发人员非常友好。

我编译了下面的源代码:

gcc demo.c -o demo -I/usr/local/include -L/usr/local/lib -lcurl

基本上,它将下载一个文件并将其保存在您的硬盘上。

文件demo.c

#include <curl/curl.h>
#include <stdio.h>

void get_page(const char* url, const char* file_name)
{
  CURL* easyhandle = curl_easy_init();

  curl_easy_setopt( easyhandle, CURLOPT_URL, url ) ;

  FILE* file = fopen( file_name, "w");

  curl_easy_setopt( easyhandle, CURLOPT_WRITEDATA, file) ;

  curl_easy_perform( easyhandle );

  curl_easy_cleanup( easyhandle );

  fclose(file);

}

int main()
{
  get_page( "http://blog.stackoverflow.com/wp-content/themes/zimpleza/style.css", "style.css" ) ;

  return 0;
}

另外,我相信您的问题与此类似:

在 C/C++ 中使用 libcurl 下载文件

于 2010-08-12T19:35:07.083 回答