Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;下载文件-如果不可用,增加内存使用率1mb/s_C++_Download_Timeout - Fatal编程技术网

C++ C++;下载文件-如果不可用,增加内存使用率1mb/s

C++ C++;下载文件-如果不可用,增加内存使用率1mb/s,c++,download,timeout,C++,Download,Timeout,如果我的代码无法到达要下载的文件,我的代码会一直填充RAM。(测试时禁用网络) 超时后如何停止下载 以下是下载的主要部分: int WINAPI WinMain(HINSTANCE instanceHandle, HINSTANCE, char*, int) { using namespace std; std::wstring loadme = targetfolder; loadme += L"\\filename.txt"; std::wstring url

如果我的代码无法到达要下载的文件,我的代码会一直填充RAM。(测试时禁用网络) 超时后如何停止下载

以下是下载的主要部分:

int WINAPI WinMain(HINSTANCE instanceHandle, HINSTANCE, char*, int)
{
    using namespace std;
    std::wstring loadme = targetfolder;
    loadme += L"\\filename.txt";
    std::wstring url1(L"fileurl");
    HRESULT hr1 = URLDownloadToFile(NULL, (url1.c_str()), (loadme.c_str()), 0, NULL); //Download-Start
}

您可以使用WinINet函数检查internet是否可用,检查url链接是否可用,并报告进度。需要“wininet.lib”

#包括
#包括
#包括
void geturl(const wchar\u t*url)
{
std::of流文件(“c:\\test\\test.htm”);
HINTERNET hopen=InternetOpen(L“myAppName”,INTERNET\u OPEN\u TYPE\u prefig,NULL,NULL,0);
如果(霍本)
{
HINTERNET hurl=InternetOpenUrl(hopen,url,NULL,0,INTERNET\u标志\u不\u缓存,0);
如果(投掷)
{
德沃德收到了;
常量int bufsize=1024;
字符buf[bufsize];
while(InternetReadFile(Hull、buf、bufsize和received))
{
//进展。。。
如果(!接收)中断;
文件写入(buf,已接收);
}
InternetCloseHandle(投掷);
}
InternetCloseHandle(霍本);
}
}
使用最后一个参数:“为了更好地控制下载及其进度,建议使用IBindStatusCallback接口。”来源:请参阅备注部分。
#include <windows.h>
#include <wininet.h>
#include <fstream>

void geturl(const wchar_t *url)
{
    std::ofstream file("c:\\test\\test.htm");
    HINTERNET hopen = InternetOpen(L"myAppName",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
    if (hopen)
    {
        HINTERNET hurl = InternetOpenUrl(hopen,url,NULL,0,INTERNET_FLAG_DONT_CACHE,0);
        if (hurl)
        {
            DWORD received;
            const int bufsize = 1024;
            char buf[bufsize];
            while (InternetReadFile(hurl, buf, bufsize, &received))
            {
                //progress...
                if (!received) break;
                file.write(buf, received);
            }
            InternetCloseHandle(hurl);
        }
        InternetCloseHandle(hopen);
    }
}