Download txt file

As a fun side project for my C++ education, I want to learn how to download a txt file from the internet using visual studio. The file I want to download is http ://data-root.illyriad.co.uk/datafile_worldmap.txt (I put a space in there because the file is really big and might crash your browser if you click on it.)

I have already scoured the internet to find usable code. The code which I found I have pasted below, but it doesn't work. I don't know anything about these libraries or how to use them, so I have come here for assistance.

The problem with the code is the #include's I believe. I don't think I have ever seen them written like that before, so I am not sure what they are meant to do differently.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>

#include <string>
#include <fstream>

int main()
{
	try
	{
		std::string url = "http://www.example.com/";
		std::ofstream out("output");

		curlpp::Cleanup cleanup;
		curlpp::Easy request;

		request.setOpt(new curlpp::options::Url(url));
		request.setOpt(new curlpp::options::WriteStream(&out));

		request.perform();
	}
	catch (cURLpp::RuntimeError &e)
	{
		std::cerr << e.what() << std::endl;
		return 1;
	}
	catch (cURLpp::LogicError &e)
	{
		std::cerr << e.what() << std::endl;
		return 1;
	}

	return 0;
}
Last edited on
Try this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#define _CRT_NONSTDC_NO_WARNINGS
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <urlmon.h>

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

using namespace std;

int main()
{
  TCHAR szUrl[] = _T("http://www.google.de");
  TCHAR szOutput[] = _T("C:\\Temp\\download.txt");

  HRESULT result = URLDownloadToFile(nullptr, szUrl, szOutput, 0, nullptr);

  switch (result)
  {
    case S_OK:
      cout << "Download ok.";
      break;
    case E_OUTOFMEMORY:
      cout << "The buffer length is invalid, or there is insufficient memory to complete the operation.";
      break;
    case INET_E_DOWNLOAD_FAILURE:
      cout << "The specified resource or callback interface was invalid.";
      break;
  }
  system("pause");
  return 0;
}


MSDN doc: https://msdn.microsoft.com/en-us/library/ms775123(v=vs.85).aspx
I'm sorry, I may need some further clarification. szUrl[] is the downloaded url, and szOutput[] is the location it is saved to correct? The code should print one of the three cases, correct? On my computer, it does not print any of these cases. Might #define _CRT_NONSTDC_NO_WARNINGS be blocking warnings windows is giving me which might tell me why this doesn't work for me? I am using Windows 7. Just taking a wild guess I thought I might have to do something with admin privileges, but running visual studio with admin privileges doesn't make this program work.

Edit: I got it to work! I just changed the internet address and the download location, and it worked. Thanks so much for your help.
Last edited on
Topic archived. No new replies allowed.