libCurl使用WriteFile写入数据WINAPI [英] libCurl write data with WriteFile WINAPI

查看:176
本文介绍了libCurl使用WriteFile写入数据WINAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用libcurl从互联网下载数据。如果使用FILE写数据下载就ok了。但我想要预分配数据并写入它。 im使用

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

以使用前缀大小数据预分配文件。之后,使用 WriteFile 写入数据下载但不成功。文件已损坏或失败,无法打开或使用它。这是我的简单代码,任何人都有想法修复writeData方法。感谢所有

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;
}


推荐答案

很多错误检查所以谁知道什么可能会出错?这是初学者用Win32e做的唯一最常见的错误。始终检查错误,如文档所述。

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.

这篇关于libCurl使用WriteFile写入数据WINAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆