1

I'm trying to make a callback function, and then write the content retrieved into a file using libcurl. The program works nicely in Linux and Windows, however, the fopen operation fails in solarix_x86. myvector contains a list of url files to be copied, and PATH_SEPARATOR is a macro which formats the string path, depending on the platform (Unix, Windows).

  vector<string>::iterator it;

  for( it=myvector.begin() ; it < myvector.end(); it++ ){
      string dest = "/home/files/" + PATH_SEPARATOR + *it // PATH_SEPARATOR IS A MACRO
      curl_easy_setopt(curl, CURLOPT_URL, it->c_str());
      curl_easy_setopt(curl, CURL_WRITEFUNCTION, write_data);

      file = fopen(dest.c_str(), "w+b");
      if(file == NULL){
          throw std::runtime_error("FILE IS NULL, CAN'T OPEN\n");
      }

      curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
      curl_easy_perform(curl);
      fclose(file);
  }

And function write_data:

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream){
     size_t written = fwrite(ptr, size, nmemb, (FILE*)stream);
     return written;
}

I'm suspecting is something related to the "w+b" flag.... Any thoughts?

************ UPDATE **********

errno=2.

cybertextron
  • 10,547
  • 28
  • 104
  • 208
  • `'w+b'` should be `"w+b"`. Single quotes = character constant. Double quotes = string constant. – BoBTFish Jul 26 '12 at 12:40
  • @BoBTFish I've changed it to "w+b", and I had the same problem – cybertextron Jul 26 '12 at 12:43
  • do you have write permission in the folder? – Rolle Jul 26 '12 at 12:52
  • @Rolle Yes, the program works correctly in `Windows` and `Lunix`. The problem only happens in `Solaris` – cybertextron Jul 26 '12 at 12:53
  • 1
    Try using perror()/strerror() to get a textual description of the error. Also, "/" works as a path separator everywhere, even on DOS and Windows; the command prompt and Explorer are the only 2 programs that deliberately forbid "/" in paths. My guess is, you are either building an incorrect path, or don't have write permissions to the file or parent directory, as Rolle said. – DanielKO Jul 26 '12 at 13:01
  • @DanielKO when i print `strerror(errno)` I get: `no such file or directory`. However, Does not the option `"w+b"` create the path for me? – cybertextron Jul 26 '12 at 13:06
  • @BoBTFish `curl` is responsible for the `http` call which retrieves the files from a remote server. Could you download the `curl` package and install it? – cybertextron Jul 26 '12 at 13:08
  • @DanielKO: The path has this format: `/home/files/import//`. `import` and `` aren't created until there is some files to be copied. – cybertextron Jul 26 '12 at 13:10
  • So do you create directories `import` and `` beforehand or do you expect `fopen()` to create directories too? – DanielKO Jul 26 '12 at 14:10
  • To be clear, "w+" will cause `fopen()` to create the file if necessary. But it won't create the missing directories in the path. – Kenster Aug 01 '12 at 13:34

1 Answers1

1

Don't confuse characters and character arrays: You want "w+b". Mind the quotation marks.

A good (or 'properly operated') compiler should have warned you about the fact that you are using a "multibyte character constant" (namely your 'w+b'), which is an obscure feature of C++ that almost never makes sense.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084