如何使用libcurl保存图像 [英] How to save image using libcurl

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

问题描述

我想使用libcurl作为一个项目,涉及从网页获取图像。
网址如下所示:



http://xxx.xxx.xxx.xxx/cgi-bin/anonymous/image.jpg



使用命令-line cURL我可以使用

  $ curl -o sampleimage.jpg http://xxx.xxx.xxx检索图片。 xxx / cgi-bin / anonymous / image.jpg 



我想知道相当于这个代码libcurl因为我现在得到的坚果。我在网上有这个示例源,它编译和东西,但我不能在任何地方看到图像文件。



这是代码:

  #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){
//打开文件
fp = fopen(google.jpg,wb);
if(fp == NULL)cout<< 文件无法打开;

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


//抓取图像
imgresult = curl_easy_perform(image);
if(imgresult){
cout<< 不能抓住图像!\\\
;
}
}

//清理资源
curl_easy_cleanup(image);
//关闭文件
fclose(fp);
return 0;
}

BTW我使用的是Mac,我在XCode上编译此代码它有一个libcurl库。



* 编辑: *问题修复。我只是使用一个完整的路径为fopen()。谢谢席!请回答问题,以便我可以选择您的正确答案。感谢!

解决方案

在打开的通话中使用完整路径,以便知道在哪里查找。



此外,你应该看看 perror 函数,这样你可以打印打开失败的原因,当它失败 - 节省了一些头痛。 p>

最后一件事:initialize fp 为null,或只有 fclose(fp)如果它是真正打开。因为它是,如果 curl_easy_init 失败,你将尝试 fclose 一个随机指针。


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!

解决方案

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 only fclose(fp) if it was really open. As it stands, if curl_easy_init fails, you'll attempt to fclose a random pointer.

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

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