title: Libcurl: image is damaged when using fstream::write instead of fwrite
body: <p>I'm trying to download image file from a url. I followed the example using <code>fwrite</code> and it succeeded. Now I'm trying to use <code>fstream::write</code> to save the data (<code>ios::binary</code>), but <strong>the data is damaged</strong>. Here is my code:</p>
  <pre><code>#include&quot;stdafx.h&quot;
  #include&lt;fstream&gt;
  #include&lt;iostream&gt;
  #include &lt;curl/curl.h&gt;
  #include &lt;string.h&gt;
  
  using namespace std;
  
  size_t write_data(void *ptr, size_t size, size_t nmemb, char* out) {
      //void *ptr, size_t size, size_t nmemb, File* fp
     
      fstream file;
      if (file.is_open()){
          file.close();
          file.clear();
      }
      file.open(out, ios::out | ios::binary);
      if (file.is_open()){
          cout &lt;&lt; &quot;open successfully\n&quot; &lt;&lt; endl;
          
          file.write((char*)ptr, nmemb*size);  // Does it correct?
      };
      // fwrite(ptr,size,nmemb,fp);
      file.close();
      file.clear();
      cout &lt;&lt;&quot;\n sizeof(ptr): &quot; &lt;&lt; sizeof(ptr) //size of ptr[0]?
           &lt;&lt;&quot;\n sizeof(char): &quot; &lt;&lt; sizeof(char)
           &lt;&lt;&quot;\n size: &quot; &lt;&lt; size
           &lt;&lt;&quot;\n nmemb: &quot; &lt;&lt; nmemb&lt;&lt; endl;
           return size*nmemb;
  }
  </code></pre>
  <p>I'm confused about the parameters in write_data.
  According to <a href="http://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html" rel="nofollow noreferrer">CURLOPT_WRITEFUNCTION</a></p>
  <blockquote>
  <p><code> size_t  write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);</code></p>
  <p>&quot;<code>ptr</code> points to the delivered data, and the size of that data is <code>size</code> multiplied with <code>nmemb</code>.&quot;</p>
  </blockquote>
  <p>......so what are the meanings of size and nmemb?</p>
  <p>When tried to download the data from a website,I printed first 3 parameters. It seems that <code>char*ptr</code> is the memory address that data stored (as 'char a[]'?), and  <code>size</code> is element's size, nmemb is number of the elements. So the data size = size * nmemb. Am I correct?</p>
  <p>The output is confusing too:</p>
  <pre><code>open successfully
  sizeof(ptr):4
  sizeof(char):1
  size:1
  nmemb:2715 
  open successfully
  sizeof(ptr):4
  sizeof(char):1
  size:1
  nmemb:4865
  download successfully
  </code></pre>
  <p><strong>When download the same url, nmemb and files' open times often change.</strong></p>
  <p>I'm also confused about 'sizeof(ptr)', it returns '4'(size of int?). How can I use the 'sizeof' to get the size of data memory, so that I can <strong>proof</strong> that <strong>data size is 'size * nmemb'?</strong></p>
  <pre><code>CURLcode download(char* url,char* out){
      CURL *curl = NULL;
      //FILE *fp = NULL;
      CURLcode res;
      curl = curl_easy_init();
      if (curl) {
          curl_easy_setopt(curl, CURLOPT_URL, url);
          curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
          curl_easy_setopt(curl, CURLOPT_WRITEDATA, out);    //fp
          res = curl_easy_perform(curl);
          curl_easy_cleanup(curl);
          return res;
      }
      else
      {
          return CURLE_FAILED_INIT;
      }
  }    
  
  int main()
  {
      CURLcode res = download(&quot;http://XXXXXX.gif&quot;, &quot;D:\\test.gif&quot;);
      if (CURLE_OK == res)
          cout &lt;&lt; &quot;download successfully.\n&quot; &lt;&lt; endl;
      else
          cout&lt;&lt;&quot;cannot download.\n&quot;&lt;&lt;endl;
      return 0;
  }
  </code></pre>
  <p>Thanks! :)</p>