gpt4 book ai didi

c - 在 C 中使用 libcurl 保存文件

转载 作者:太空狗 更新时间:2023-10-29 15:01:27 26 4
gpt4 key购买 nike

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

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

最佳答案

我找到了 this resource对开发人员非常友好。

我编译了下面的源代码:

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

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

Download file using libcurl in C/C++

关于c - 在 C 中使用 libcurl 保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3471122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com