使用C中的libcurl保存文件

Pat*_*Pat 5 c curl get http libcurl

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

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

kar*_*lip 6

我发现这个资源非常适合开发人员.

我编译了下面的源代码:

gcc demo.c -o demo -I/usr/local/include -L/usr/local/lib -lcurl
Run Code Online (Sandbox Code Playgroud)

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

文件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;
}
Run Code Online (Sandbox Code Playgroud)

另外,我相信你的问题类似于这个问题:

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