2

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!

Casey
  • 38,510
  • 6
  • 83
  • 116
Igor Tupitsyn
  • 1,103
  • 1
  • 16
  • 39
  • is your write function being called? - FYI I just copied this verbatim and dumped it into XCode and ran it and it downloaded the file. It downloads it to the location you are running it out of. – NG. Aug 17 '13 at 00:44
  • SB, thanks! I did it again and it worked. However, now I have a txt file (I replaced .gz with .txt) in the root directory. Do you know how it would be possible to parse the content of the file to a string instead without saving this as a text file? Again, thank you so much! – Igor Tupitsyn Aug 17 '13 at 01:06
  • Check out http://stackoverflow.com/questions/2912520/read-file-contents-into-a-string-in-c – NG. Aug 17 '13 at 01:13

1 Answers1

4

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";
}
Casey
  • 38,510
  • 6
  • 83
  • 116
  • Thanks, Casey. I will try it and will let you know if it worked as soon as I solve another issue I faced. I will open a new topic but will get back to it asap. Thanks! – Igor Tupitsyn Aug 17 '13 at 13:54