IBG*_*IBG 6 c c++ xcode curl libcurl
我想将libcurl用于涉及从网页获取图像的项目.URL如下所示:
http://xxx.xxx.xxx.xxx/cgi-bin/anonymous/image.jpg
使用命令行cURL我可以使用检索图像
$curl -o sampleimage.jpg http://xxx.xxx.xxx.xxx/cgi-bin/anonymous/image.jpg
Run Code Online (Sandbox Code Playgroud)
我想知道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 ){
// 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;
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,我正在使用Mac,而我正在XCode上编译这个代码,它有一个libcurl库.
*编辑:*问题已修复.我刚刚使用了fopen()的完整路径.谢谢你!请回答这个问题,以便我可以选择你的正确答案.谢谢!
在公开征集中使用完整路径,以便您知道要查找的位置。
此外,您还应该查看该perror函数,以便在打开失败时打印出打开失败的原因 - 省去一些麻烦。
最后一件事:初始化fp为 null,或者仅fclose(fp)当它确实打开时才初始化。按照目前的情况,如果curl_easy_init失败,您将尝试fclose随机指针。
| 归档时间: |
|
| 查看次数: |
6961 次 |
| 最近记录: |