title: libcurl output to variable instead of textfile
body: <p>In failing to get <code>curlpp</code> for C++ working, I have decided to start using <code>libcurl</code> with C instead (for now). Being completely new to both C and C++, this is getting a little confusing. I'm not even sure if I can keep C and C++ functions apart, but as far as I know, this is pure C.</p>
  
  <p>With the help of a friend, I have managed to write the output (page contents that curl picked up) to a textfile, but I want to put it in a string variable instead, so I can use the output in other parts of the code. I could just re-open the textfile and read its contents but that's silly, I want to stop writing to a file and just save to a string variable immediately.</p>
  
  <p><strong>The write function</strong></p>
  
  <pre><code>/* the function to invoke as the data received */
  size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
      size_t written;
      written = fwrite(ptr, size, nmemb, stream);
      return written;
  }
  </code></pre>
  
  <p><strong>The entire code</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "curl/curl.h"
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include &lt;iostream&gt;
  using namespace std;
  
  /* the function to invoke as the data recieved */
  size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
      size_t written;
      written = fwrite(ptr, size, nmemb, stream);
      return written;
  }
  
  int main(int argc, char *argv[]) {
  
      CURL *curl;
      FILE *fp;
    CURLcode res;
  
    curl = curl_easy_init();
  
  char outfilename[FILENAME_MAX] = "C:\\Users\\admin\\Downloads\\bbb.txt";
  
    if(curl) {
  char *response = NULL;
  
          fp = fopen(outfilename,"wb");
          curl_easy_setopt(curl, CURLOPT_URL, "http://www.*hidden*.org/wp-test/lalala.txt");
          curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
          curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
          res = curl_easy_perform(curl);
  
          curl_easy_cleanup(curl);
          fclose(fp);
  
  
    }
      return 0;
  }
  </code></pre>
  
  <p>I was also hoping someone could elaborate on how this function is used, exactly. I am used (from php and vb.net) to a function like this:</p>
  
  <pre><code>function1(ThisIsAvariable,ThisIsAvariableToo)
  {
      if ThisIsAvariable = something
      {
           Print "gogopowerrrangers" *append* ThisIsAvariableToo
      }
  };
  </code></pre>
  
  <p>Which would then be used like:</p>
  
  <pre><code>function1(PassThisAsVariable1,PassThisAsVariable2);
  </code></pre>
  
  <p>But in the code above, the function</p>
  
  <pre><code>size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
  </code></pre>
  
  <p>Is simply called like this</p>
  
  <pre><code>write_data
  </code></pre>
  
  <p>As you can see here:</p>
  
  <pre><code>curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  </code></pre>
  
  <p>So what's all this for?</p>
  
  <pre><code>(void *ptr, size_t size, size_t nmemb, FILE *stream)
  </code></pre>
  
  <p>Does curl automatically "fill" those in or does C/C++ work differently with functions than most other languages?</p>