Q:

libCurl write data with WriteFile WINAPI

i using libcurl to download data form internet. if using FILE to write data downloaded is ok. but i want pre-allocate data and write it. im using 
CreateFile
SetFilePointer
SetEndOfFile
to pre-allocate file with prefix size data. After that im using WriteFile to write data downloaded but not successful. File is corrupt or fail, cannot open or using it. This is my simple code, anyone have idea to fix writeData method. Thanks 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 to write
        (size*nmemb),  // number of bytes to write
        0, // number of bytes that were 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;
}

A:

You don't perform very much error checking so who knows what could be going wrong? This is the single most common mistake that beginners make with Win32e. Always check for errors, as described by the documentation. 
Looking at your code, you don't move the file pointer back to the beginning of the file before starting to write to the file. That alone means you code cannot work. Perhaps there are more errors, but I've not dug any deeper. 
You would do well to perform some debugging. Trying to solve problems by staring at code doesn't always succeed. Inspect the program whilst it executes. Learn how to debug. 

