title: How can I CURL POST a file using file pointer in C++
body: <p>I have a file pointer, such as the following:</p>
  
  <pre><code>FILE* f = tmpfile()
  </code></pre>
  
  <p>How do I use libcurl to do a HTTP POST to a URL as a field named F1?</p>
  
  <p>I tried reading the file contents into a char* array but and used the following to upload:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include &lt;curl/curl.h&gt;
  #include &lt;curl/types.h&gt;
  #include &lt;curl/easy.h&gt;
  
  char* dump_buffer(void *buffer, int buffer_size){
  int i;
  char *ch = malloc(buffer_size);
  for(i = 0;i &lt; buffer_size;++i){
      ch[i] = ((char *)buffer)[i];
      //printf("%c",((char *)buffer)[i]);
  }
  return ch;
  }
  
  char* readFileBytes(const char *name){
  FILE *file;
  char *buffer;
  unsigned long fileLen;
  int i;
  file = fopen("index.tar", "rb");
  if (!file)
  {
      fprintf(stderr, "can't open file %s", "1.m4v");
      exit(1);
  }
  
  fseek(file, 0, SEEK_END);
  fileLen=ftell(file);
  fseek(file, 0, SEEK_SET);
  
  buffer=(char *)malloc(fileLen+1);
  
  if (!buffer)
  {
      fprintf(stderr, "Memory error!");
      fclose(file);
      exit(1);
  }
  
  fread(buffer, fileLen, 1, file);
  fclose(file);
  char* ret = dump_buffer(&amp;buffer, fileLen);
  
  for(i = 0;i &lt; fileLen;++i){
      //printf("%c",ret[i]);
  }
  
  return ret;
  }
  
  
  int main(int argc, char *argv[])
  {
    CURL *curl;
    CURLcode res;
  
    struct curl_httppost *formpost=NULL;
    struct curl_httppost *lastptr=NULL;
  struct curl_slist *headers=NULL;
  headers = curl_slist_append(headers, "Content-Type: multipart/form-data");
  
    curl_global_init(CURL_GLOBAL_ALL);
  
    /* Fill in the filename field */
  
  char* p = readFileBytes("index.tar");
  
  curl_formadd(&amp;formpost,
               &amp;lastptr,
               CURLFORM_COPYNAME, "F2",
               CURLFORM_FILE, "index.tar",
               CURLFORM_END);
  
  curl_formadd(&amp;formpost,
               &amp;lastptr,
               CURLFORM_COPYNAME, "F1",
               CURLFORM_COPYCONTENTS, (char*)p,
               CURLFORM_END);
  curl = curl_easy_init();
    /* initalize custom header list (stating that Expect: 100-continue is not
   wanted */
    if(curl) {
  /* what URL that receives this POST */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  curl_easy_setopt(curl, CURLOPT_URL, "http://oceanfizz.usc.edu/upload.php");
  curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  res = curl_easy_perform(curl);
  
  /* always cleanup */
  curl_easy_cleanup(curl);
  
  /* then cleanup the formpost chain */
  curl_formfree(formpost);
  /* free slist */
    }
    return 0;
  }
  </code></pre>
  
  <p>The output that I get is </p>
  
  <pre><code>guest-wireless-207-151-246-070:Desktop ankurcha$ ./postit2
  * About to connect() to oceanfizz.usc.edu port 80 (#0)
  *   Trying 128.125.49.29... * connected
  * Connected to oceanfizz.usc.edu (128.125.49.29) port 80 (#0)
  &gt; POST /upload.php HTTP/1.1
  Host: oceanfizz.usc.edu
  Accept: */*
  Content-Length: 20770
  Expect: 100-continue
  Content-Type: multipart/form-data; boundary=----------------------------e04b6194f620
  
  &lt; HTTP/1.1 100 Continue
  &lt; HTTP/1.1 200 OK
  &lt; Date: Mon, 19 Apr 2010 03:51:04 GMT
  &lt; Server: Apache/2.2.12 (Ubuntu)
  &lt; X-Powered-By: PHP/5.2.10-2ubuntu6.4
  &lt; Vary: Accept-Encoding
  &lt; Content-Length: 335
  &lt; Content-Type: text/html
  &lt; 
  array(1) {
    ["F2"]=&gt;
    array(5) {
  ["name"]=&gt;
  string(9) "index.tar"
  ["type"]=&gt;
  string(24) "application/octet-stream"
  ["tmp_name"]=&gt;
  string(14) "/tmp/phpyOiqXh"
  ["error"]=&gt;
  int(0)
  ["size"]=&gt;
  int(20480)
    }
  }
  array(1) {
    ["F1"]=&gt;
    string(0) ""
  }
  Sorry, there was a problem uploading your file. 
  * Connection #0 to host oceanfizz.usc.edu left intact
  * Closing connection #0
  </code></pre>
  
  <p>I was expecting F1 to have binary content.</p>