您的位置:首页 > 编程语言 > C语言/C++

C++ 下使用curl 获取ftp文件

2014-06-07 11:07 369 查看
http://curl.haxx.se/下载的win32版本的curl都不能使,#include <curl.h>后总是报错:external symbol ,意思就是没有链接到curl的各种library,最后尝试了n多次终于成功了,希望大家以后少走弯路!

以Windows 8 64bit,Visual Studio 2012 为例:

1、获取curl:

这里可以获得所有版本的curl,我下载的是Win32
- MSVC
版本的curl,我下载的是MSVC 7.19.3 devl SSL 版本,可以从这里下载。



2、解压后得到一个文件夹“libcurl-7.19.3-win32-ssl-msvc”,可以看到里面有include文件夹和lib文件夹

3、添加引用:用Visual Studio打开你的C++工程,选择Project ->project name Properties -> Configuration
Properties -> VC++ Dicrectories 在右侧的Include Directories添加“libcurl-7.19.3-win32-ssl-msvc\include”的绝对路径,如:“X:\a\b\c\libcurl-7.19.3-win32-ssl-msvc\include”,其中“X:\a\b\c\”根据你curl解压到的地方不同,要有所变动。





同理,将“libcurl-7.19.3-win32-ssl-msvc\lib\debug”的绝对路径添加到Libraries Directories中,注意libcurl-7.19.3-win32-ssl-msvc\lib文件夹中包含debug和release两个文件夹,这里之所以添加debug文件夹是因为笔者当前vs的编译模式是debug的,如果你当前的编译模式是release的,那么请将libcurl-7.19.3-win32-ssl-msvc\lib\release文件夹添加到这里,而不要添加debug。

注:在哪种编译模式下就添加哪个文件夹!要把绝对路径写上,而不是相对路径!如果你不知道什么是绝对路径,请看这里



4、添加lib:选择左侧的Configuration Properties -> Linker -> Input 将Additional Dependencies中加入:

curllib.lib和curllib_static.lib这两个,然后保存



5、拷贝dll:将libcurl-7.19.3-win32-ssl-msvc文件夹下所有的.dll文件拷到VS工程目录下的Debug文件夹中。注意,这里之所以拷到VS工程Debug文件夹中,是因为VS编译后的.exe运行时侯会寻找.dll,如果不放过来就该提示有错误了。另外,VS
2012 编译后可能会生成2个或2个以上的Debug文件夹,但只有一个Debug里有编译后生成的.exe文件,要放到这个Debug里,不要放错。PS,据说将.dll放到C盘System32文件夹里可以一劳永逸,不用每次新建工程都要拷贝.dll到Debug下,不过有待考证,我没试过。

6、关于libsasl.dll:此时运行可能会提示你缺少libsasl.dll,这个dll下载的curl里是没有的,可以从网上下一个,然后和其他的.dll一样放到Debug目录下,最后编译就可以运行了

7、获取ftp文件的示例:这里是一份获取FTP服务器文件的示例代码,可以测试curl是否配置成功(前提是你要有个FTP服务器,如果你没有,但是想搭建一个简单的FTP服务器,请看这里

要修改"ftp://192.168.1.101/sc.jpg"和"username:userpassword"两个参数,这分别对应着FTP服务器上文件的地址,和FTP服务器的用户名和密码,注意用户名和密码中间有个冒号分隔。

8、Good luck!

#include <string>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <curl\curl.h>
#include <stdio.h>
using namespace std;

struct FtpFile
{
const char *filename;
FILE *stream;
};

static size_t FetchFiles(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out = (struct FtpFile *)stream;
if (out && !out->stream)
{
// open file for writing
out->stream = fopen(out->filename, "wb");
if (!out->stream)
return -1; // failure, can't open file to write
}
return fwrite(buffer, size, nmemb, out->stream);
}

int DownloadFtpFile()
{
CURL *curl;
CURLcode res;
struct FtpFile ftpfile = {
"a.jpg", // name to store the file as if succesful//
NULL
};

curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();

if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL,"ftp://192.168.1.101/sc.jpg");
curl_easy_setopt(curl, CURLOPT_USERPWD, "username:userpassword");
// Define our callback to get called when there's data to be written //
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, FetchFiles);
// Set a pointer to our struct to pass to the callback //
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);

// Switch on full protocol/debug output //
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

res = curl_easy_perform(curl);

// always cleanup
curl_easy_cleanup(curl);

if (CURLE_OK != res)
{
//we failed
fprintf(stderr, "curl told us %d\n", res);
}
}

if (ftpfile.stream)
fclose(ftpfile.stream); // close the local file

curl_global_cleanup();

getchar();

return 0;
}

int main(void)
{
DownloadFtpFile();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: