如何使用 libcurl 保存图像
我想将 libcurl 用于一个涉及从网页获取图像的项目。 URL 如下所示:
http://xxx.xxx.xxx.xxx /cgi-bin/anonymous/image.jpg
使用命令行 cURL,我可以使用“我想知道 libcurl 中此代码的等效项”来检索图像,
$curl -o sampleimage.jpg http://xxx.xxx.xxx.xxx/cgi-bin/anonymous/image.jpg
因为我现在快疯了。我在网上得到了这个示例源代码,它可以编译等等,但我在任何地方都看不到图像文件。
这是代码:
#include <iostream>
#include <curl/curl.h>
#include <stdio.h>
using namespace std;
int main(){
CURL *image;
CURLcode imgresult;
FILE *fp;
image = curl_easy_init();
if( image ){
// Open file
fp = fopen("google.jpg", "wb");
if( fp == NULL ) cout << "File cannot be opened";
curl_easy_setopt(image, CURLOPT_URL, "http://192.168.16.25/cgi-bin/viewer/video.jpg");
curl_easy_setopt(image, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(image, CURLOPT_WRITEDATA, fp);
// Grab image
imgresult = curl_easy_perform(image);
if( imgresult ){
cout << "Cannot grab the image!\n";
}
}
// Clean up the resources
curl_easy_cleanup(image);
// Close the file
fclose(fp);
return 0;
}
顺便说一句,我使用的是 Mac,我在具有 libcurl 库的 XCode 上编译此代码。
*编辑:*问题已解决。我刚刚使用了 fopen() 的完整路径。谢谢马特!请回答问题,以便我选择您的作为正确答案。谢谢!
I wanted to use libcurl for a project which involves getting an image from a webpage.
The URL looks like this:
http://xxx.xxx.xxx.xxx/cgi-bin/anonymous/image.jpg
Using command-line cURL I can retrieve the image using
$curl -o sampleimage.jpg http://xxx.xxx.xxx.xxx/cgi-bin/anonymous/image.jpg
I want to know the equivalent of this code in libcurl because I'm getting nuts right now. I got this sample source on the net, and it compiles and stuff, but I can't see the image file anywhere.
This is the code:
#include <iostream>
#include <curl/curl.h>
#include <stdio.h>
using namespace std;
int main(){
CURL *image;
CURLcode imgresult;
FILE *fp;
image = curl_easy_init();
if( image ){
// Open file
fp = fopen("google.jpg", "wb");
if( fp == NULL ) cout << "File cannot be opened";
curl_easy_setopt(image, CURLOPT_URL, "http://192.168.16.25/cgi-bin/viewer/video.jpg");
curl_easy_setopt(image, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(image, CURLOPT_WRITEDATA, fp);
// Grab image
imgresult = curl_easy_perform(image);
if( imgresult ){
cout << "Cannot grab the image!\n";
}
}
// Clean up the resources
curl_easy_cleanup(image);
// Close the file
fclose(fp);
return 0;
}
BTW I'm using a Mac and I'm compiling this code on XCode which has a libcurl library.
*EDIT:*Problem fixed. I just used a full path for the fopen(). Thanks Mat! Please answer the question so that I can choose yours as the correct answer. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在公开征集中使用完整路径,以便您知道要查找的位置。
另外,您应该查看
perror
函数,以便在打开失败时打印出打开失败的原因 - 省去一些麻烦。最后一件事:将
fp
初始化为 null,或者仅将fclose(fp)
(如果它确实打开)初始化。就目前情况而言,如果curl_easy_init
失败,您将尝试fclose
随机指针。Use a full path in the open call so that you know where to look.
Also you should look at the
perror
function so you can print the reason why the open fails when it does - saves a few headaches.Last thing: initialize
fp
to null, or onlyfclose(fp)
if it was really open. As it stands, ifcurl_easy_init
fails, you'll attempt tofclose
a random pointer.