C ++ libcurl - 从ftp下载一个txt文件并保存为字符串

时间:2013-08-17 00:41:06

标签: c++ text ftp download libcurl

我正在从Java切换到C ++,这真的非常痛苦。我无法从ftp下载文件。我正在尝试下载包含一些文本的文本文件,我只想将此文本保存为字符串而不保存文件。

我从libcurl网站获得了以下代码:

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


  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) {
 /* 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 main(void)
  {
   CURL *curl;
   CURLcode res;
   struct FtpFile ftpfile={
   "curl.tar.gz", /* 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://tgftp.nws.noaa.gov/data/observations/metar/stations/KLAX.TXT");
   /* Define our callback to get called when there's data to be written */
   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
   /* 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();

   return 0;
   }

不幸的是,它的作用是提供了很多关于我的连接状态和文件本身的信息,但它实际上并没有给我文件的内容。我究竟做错了什么?另外,我如何将内容保存在变量中?

非常感谢!

1 个答案:

答案 0 :(得分:2)

不是将回调函数传递给将检索到的数据写入文件的libcurl,而是传递一个将数据附加到字符串的函数(未经测试):

#include <iostream>
#include <string>
#include <curl/curl.h>

static size_t my_write(void *buffer, size_t size, size_t nmemb, void *param)
{
  std::string& text = *static_cast<std::string*>(param);
  size_t totalsize = size * nmemb;
  text.append(static_cast<char*>(buffer), totalsize);
  return totalsize;
}

int main()
{
  std::string result;
  CURL *curl;
  CURLcode res;

  curl_global_init(CURL_GLOBAL_DEFAULT);

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KLAX.TXT");
    /* Define our callback to get called when there's data to be written */
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write);
    /* Set a pointer to our struct to pass to the callback */
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);

    /* 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 */
      std::cerr << "curl told us " << res << '\n';
    }
  }
  curl_global_cleanup();

  std::cout << result << "\n\n";
}