简体   繁体   中英

C++ libcurl — download a txt file from ftp and save as a string

I am switching from Java to C++ and it is really very painful. I am stuck with downloading files from ftp. I am trying to download a text file which includes some text and I just want to save this text as a string without saving the file.

I got the following code from the libcurl site:

   #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;
   }

Unfortunately, what it does is it provides a lot of information about the status of my connection and the files itself but it does not actually give me the content of the file. What am I doing wrong? Also, how would I save the content in a variable?

Many many thanks!

Instead of passing a callback function to libcurl that writes the retrieved data to a file, pass a function that appends the data to a string (untested):

#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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM