Does someone know how to download a file in c++? A simple way. i just need it to download and save it to a specific directory.

Recommended Answers

All 4 Replies

While I can't guess why the c tag, you left out where the file is. That is, native C++ doesn't have the utilities to work with the web. Checking out https://www.codespeedy.com/how-to-download-files-from-internet-using-cpp/ confirms this.

You not only left out that but what if the file is on FTP server, web page or something else. How you approach this varies with where the file is now. As to the specific directory, that would be in your code. Given the first link in my reply the answer is yes. But simple? No.

You can use URLDownloadToFileA. It has worked for me very well.

This is what I do in my Noti-Gang: DrDisRespect Edition Windows App. It's pretty simple. It downloads a file and saves it to a specific folder.

#include <iostream>
#include <urlmon.h>

#pragma comment(lib, "urlmon.lib")

using namespace std;

int main()
{
    system("mkdir C:\\Noti-Gang\\data\\");
    string savepath = "C:\\Noti-Gang\\data\\drdisrespect_alert.wav";
    string dwnld_URL = "https://github.com/toneewa/Noti-Gang/raw/main/drdisrespect_alert.wav";
    URLDownloadToFileA(NULL, dwnld_URL.c_str(), savepath.c_str(), 0, NULL);
    std::cout << savepath << std::endl;
    return 0;
}

I was able to find URLDownloadToFile() but what is URLDownloadToFileA()? Is that a typo or a function you wrote?

commented: No typo. It's a built-in fuction. You'll see it in the header urlmon.h. Rare. It's not talked about much, but I remember switching to fix old code. +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.