I'm working on a basic downloader based on the http client tutorial. I've tried to match it as closely as possible, with the exception of the open_ostream call which takes a Windows::Storage::StorageFile instead of a path string. It does the download and save to file correctly, but when I try to close the file stream it throws an exception because fileInfo is NULL in _close_file in filestream.h.
auto fileStream = std::make_shared<ostream>();
Windows::Storage::StorageFolder^ storageRoot = Windows::Storage::ApplicationData::Current->TemporaryFolder;
pplx::task<void> createFileTask = create_task(storageRoot->CreateFileAsync(fileName_w, Windows::Storage::CreationCollisionOption::ReplaceExisting))
.then([=](Windows::Storage::StorageFile^ storageFile)
{
return fstream::open_ostream(storageFile);
}).then([=](ostream outFile)
{
*fileStream = outFile;
web::http::client::http_client client(url_u);
return client.request(web::http::methods::GET);
}).then([=](web::http::http_response response)
{
m_lastStatusCode = response.status_code();
return response.body().read_to_end(fileStream->streambuf());
}).then([=](size_t)
{
return fileStream->close(); // breaks here
});
try
{
createFileTask.wait();
}
catch (const std::exception &e)
{
printf("Error exception:%s\n", e.what());
}