Link to home
Start Free TrialLog in
Avatar of s_federici
s_federici

asked on

building error messages

In reality this question is about Visual C 6.0 but I couldn't find a more suitable area. When trying to build source code from http://curl.haxx.se/lxr/source/docs/examples/httpput.c, I get the following error messages

C:\Project\libcurl\VisualStudio\MyApplication2\MyApplication.cpp(35) : error C2664: 'fread' : cannot convert parameter 4 from 'void *' to 'struct _iobuf *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
C:\Project\libcurl\VisualStudio\MyApplication2\MyApplication.cpp(60) : error C2065: 'open' : undeclared identifier
C:\Project\libcurl\VisualStudio\MyApplication2\MyApplication.cpp(62) : error C2065: 'close' : undeclared identifier

What is the source of these errors?

Source code follows:

#include "stdafx.h"
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>

#include <curl/curl.h>

/*
 * This example shows a HTTP PUT operation. PUTs a file given as a command
 * line argument to the URL also given on the command line.
 *
 * This example also uses its own read callback.
 *
 * Here's an article on how to setup a PUT handler for Apache:
 * http://www.apacheweek.com/features/put
 */

size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
  size_t retcode;

  /* in real-world cases, this would probably get this data differently
     as this fread() stuff is exactly what the library already would do
     by default internally */
  retcode = fread(ptr, size, nmemb, stream);

  fprintf(stderr, "*** We read %d bytes from file\n", retcode);

  return retcode;
}

int main(int argc, char **argv)
{
  CURL *curl;
  CURLcode res;
  FILE * hd_src ;
  int hd ;
  struct stat file_info;

  char *file;
  char *url;

  if(argc < 3)
    return 1;

  file= argv[1];
  url = argv[2];

  /* get the file size of the local file */
  hd = open(file, O_RDONLY) ;
  fstat(hd, &file_info);
  close(hd) ;

  /* get a FILE * of the same file, could also be made with
     fdopen() from the previous descriptor, but hey this is just
     an example! */
  hd_src = fopen(file, "rb");

  /* In windows, this will init the winsock stuff */
  curl_global_init(CURL_GLOBAL_ALL);

  /* get a curl handle */
  curl = curl_easy_init();
  if(curl) {
    /* we want to use our own read function */
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);

    /* enable uploading */
    curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;

    /* HTTP PUT please */
    curl_easy_setopt(curl, CURLOPT_PUT, TRUE);

    /* specify target URL, and note that this URL should include a file
       name, not only a directory */
    curl_easy_setopt(curl,CURLOPT_URL, url);

    /* now specify which file to upload */
    curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);

    /* and give the size of the upload */
    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, file_info.st_size);

    /* Now run off and do what you've been told! */
    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  fclose(hd_src); /* close the local file */

  curl_global_cleanup();
  return 0;
}


Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Knut Hunstad
Knut Hunstad
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of s_federici
s_federici

ASKER

Thanks a lot. It worked immediately. Could you please expand on the reasons why the error raised? A missing library I suppose, but which one?
Avatar of DanRollins
s_federici,
If that does not solve it, let me know and I'll download the cURL devkit and see if I can reproduce the problem.
Hi Dan,
khun's suggestion worked well. But I need lot of help about how to effectively use libcurl to upload files to a website behind a firewall that doesn't allow for FTP. I red some documentation about very complicated ways to allow Apache servers to accept http PUT requests. Is there a clean way our there to upload a file to whatever website allowing FTP access, overcoming FTP limitations behind firewalls? Why shouldn't I be allowed to send my files somewhere if I'm allowed to upload them to a site via HTML forms? If you think you can answer my question, I can open a brand new question for you.
My suggestion is: don't spend time speculating over this if all you want is getting it to work! There are some variations around open/_open/fopen etc. from different compilers and also between different versions of the same compiler. If you get problems, though, you might need to follow up on this.

For your next question, it would be more appropriate to start a new question. You might also try asking at some of the forums at curl.haxx.se, seems to be quite many users of this out there! I just took a quick look...