c++ - libCurl write data with WriteFile WINAPI -


i using libcurl download data form internet. if using file write data downloaded ok. want pre-allocate data , write it. im using

createfile

setfilepointer

setendoffile

to pre-allocate file prefix size data. after im using writefile write data downloaded not successful. file corrupt or fail, cannot open or using it. simple code, have idea fix writedata method. all

#include "stdafx.h" #include <stdio.h> #include <iostream> #include <fstream> #include <string> #include <conio.h>  #include <curl\curl.h>  using namespace std; size_t writedata(void *buffer, size_t size, size_t nmemb, handle *userdata){         bool berrorflag = false;     berrorflag = writefile(         userdata,           // open file handle         buffer,      // start of data write         (size*nmemb),  // number of bytes write         0, // number of bytes written         null);       return nmemb;    } int progressdata(void *ptr, double dltotal, double dlnow, double ultotal, double ulnow){         cout << "downloaded: " << dlnow / 1024 << "kb  total size: " << dltotal / 1024 << "kb" << endl;     return 0; }   int _tmain(int argc, _tchar* argv[]) {     curl *curl;      file *fp;     curlcode res;     string url = "http://myserver.allmedia.com/games/ngaokiem300115/samplefile.zip";     string save = "e:\\downloads\\samplefile.zip";                handle file = createfile("e:\\downloads\\samplefile.zip", generic_read | generic_write, 0, null, open_always, file_attribute_normal, null);     if (file == invalid_handle_value){         cout << "allocate file fail" << endl;            }      setfilepointer(file, 486702722, null, file_begin);     setendoffile(file);     curl = curl_easy_init();     if (curl){          fp = fopen(save.c_str(), "wb");         curl_easy_setopt(curl, curlopt_url, url);         curl_easy_setopt(curl, curlopt_writefunction, writedata);         curl_easy_setopt(curl, curlopt_writedata, &file);          curl_easy_setopt(curl, curlopt_progressfunction, progressdata);         curl_easy_setopt(curl, curlopt_noprogress, false);                res = curl_easy_perform(curl);         curl_easy_cleanup(curl);      }            system("pause");     return 0; } 

you don't perform error checking knows going wrong? single common mistake beginners make win32e. check errors, described documentation.

looking @ code, don't move file pointer beginning of file before starting write file. alone means code cannot work. perhaps there more errors, i've not dug deeper.

you perform debugging. trying solve problems staring @ code doesn't succeed. inspect program whilst executes. learn how debug.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -