Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


building error messages

Posted on 2006-05-18
Medium Priority
Last Modified: 2008-02-01
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 */

  /* 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 */
  fclose(hd_src); /* close the local file */

  return 0;

Question by:s_federici
  • 2
  • 2

Accepted Solution

Knut Hunstad earned 1500 total points
ID: 16716280
Some suggestions, not tested:

I'm not sure how the read_callback is used, but if it sends the FILE* of the file as a void*, you could simply cast it in fread, I think:

retcode = fread(ptr, size, nmemb, (FILE*) stream);

That should remove the first error. To remove the next, replacing with:

hd = _open(file, O_RDONLY) ;
  _fstat(hd, &file_info);
  _close(hd) ;

might do the trick (underlines).

Author Comment

ID: 16716524
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?
LVL 49

Expert Comment

ID: 16716532
If that does not solve it, let me know and I'll download the cURL devkit and see if I can reproduce the problem.

Author Comment

ID: 16716566
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.

Expert Comment

by:Knut Hunstad
ID: 16716616
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...

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Integration Management Part 2
Loops Section Overview
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question