windows10 + vs2010使用curl(command URL)库
curl库的编译可以参考:http://blog.youkuaiyun.com/neverup_/article/details/21961017
openssl解压要使用管理员权限解压(不会请百度),不然会出现符号链接错误。
具体的使用可以看curl官网提供的API和example: https://curl.haxx.se/libcurl/c/example.html
把一个txt文件上传我的ftp
#include <iostream>
#include <string>
#include <curl/curl.h>
using namespace std;
#pragma comment(lib, "libcurld_imp.lib")
int main()
{
CURL *curl;
CURLcode res;
FILE *fd;
double speed_upload, total_time;
fd = fopen("test.txt", "rb");
if(!fd){
printf("read failed!");
return 1;
}
curl = curl_easy_init();
if(curl){
curl_easy_setopt(curl, CURLOPT_URL, "ftp://192.168.0.100/myfile.txt");
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK){
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
else {
curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
fprintf(stderr, "Speed: %.3f bytes/sec during %.3f secondes\n", speed_upload, total_time);
}
curl_easy_cleanup(curl);
}
fclose(fd);
return 0;
}
一个注意点就是
curl_easy_setopt(curl, CURLOPT_URL,”ftp://192.168.0.100/myfile.txt“);直接填写url会出现Uploading to a URL without a file name!
解决方案就是在后面加上文件在ftp上的名字
参考:http://stackoverflow.com/questions/16429849/libcurl-code-not-working-says-im-uploading-to-the-server-w-out-a-file-name
成功上传