如果想要成功调用libcurl库,则需要将它所依赖的头文件和库文件加载到项目中,如果有需要的话请点击此处获取libcurl库

https://pan.baidu.com/s/1ZGaw0NAhmso2Tbt9DGLHwQ 

#include <stdio.h>
#include <stdlib.h>
#include <curl.h>

#undef DISABLE_SSH_AGENT

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

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
                        void *stream)
{
  struct FtpFile *out=(struct FtpFile *)stream;
  if(out && !out->stream) {
    out->stream=fopen(out->filename, "wb");
    if(!out->stream)
      return -1; 
  }
  return fwrite(buffer, size, nmemb, out->stream);
}

int main(void)
{
  CURL *curl;
  CURLcode res;
  const char* urlkey = "用户名:密码"; //服务器用户名及密码
  const char* ServerIpPath = "sftp://服务器IP//服务端文件路径";
  struct FtpFile ftpfile={"本地文件路径", NULL};
  
  curl_global_init(CURL_GLOBAL_DEFAULT);


  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL,ServerIpPath);
    curl_easy_setopt(curl, CURLOPT_USERPWD,urlkey);  
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);


#ifndef DISABLE_SSH_AGENT
    curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_PASSWORD);
#endif

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    res = curl_easy_perform(curl);

    curl_easy_cleanup(curl);

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

  if(ftpfile.stream)
    fclose(ftpfile.stream); 

  curl_global_cleanup();


  return 0;
}

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐