使用Ccurve中的libcurl保存文件 [英] Saving a file using libcurl in C

查看:190
本文介绍了使用Ccurve中的libcurl保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从perl扩展到C,我试图使用curl的库,只是保存文件从远程url,但我很难找到一个很好的例子来工作。

I'm expanding from perl to C and I'm trying to use curl's library to simply save a file from a remote url but I'm having a hard time finding a good example to work from.

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

Also, I'm not sure if I should be using curl_easy_recv or curl_easy_perform

推荐答案

我发现此资源非常友好。

我编译了以下源代码:

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

基本上,并将其保存在硬盘上。

Basically, it will download a file and save it on your hard disk.

文件 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;
}

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

Also, I believe your question is similar to this one:

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

这篇关于使用Ccurve中的libcurl保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆