title: Download an image from an url using curl in c++
body: <p>I am doing a project and it's first step includes to download an image from an url and save it a some location.Further processing will be done on that image at the later stage.For this purpose I am using curl libraries in visual studio along with opencv.I am new to using curl libraries.I saw an answer <a href="https://stackoverflow.com/questions/10112959/download-an-image-from-server-curl-however-taking-suggestions-c">here</a>. But, I can't understand it.Here is my code</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;curl/curl.h&gt;
  #include &lt;curl/types.h&gt;
  #include &lt;curl/easy.h&gt;
  #include &lt;string&gt;
  
  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(void)
  {
      CURL* curl;
      FILE* fp;
      CURLcode res;
      char* url = &quot;http://pimg.tradeindia.com/01063301/b/1/CRO-Oscilloscope.jpg&quot;;
      char outfilename[FILENAME_MAX] = &quot;C:\\bbb.jpg&quot;;
      curl = curl_easy_init();
      if (curl)
      {
          fp = fopen(outfilename, &quot;wb&quot;);
          curl_easy_setopt(curl, CURLOPT_URL, url);
          curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
          curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
          curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
          res = curl_easy_perform(curl);
          /* always cleanup */
          curl_easy_cleanup(curl);
          fclose(fp);
      }
      return 0;
  }
  </code></pre>
  <p>Thanks for the help.</p>