Text From A .TXT File (HTTP)

Hi Guys!

I am trying to obtain text from a .txt file online. The .txt file either contains "Yes" or "No". Is there a way I can download using winsock2.h or ws2_32.lib?

So basically im trying to get "Yes" or "No" into a variable. I have read a lot on this subject and I am prepared to read more if someone can point me in the right direction.

I am trying to keep the compiled file size down, so using,

#include <iostream>
#include <fstream>

is out of the question for me.


Many Thanks,

Matt.
Last edited on
Is there a way I can download using winsock2.h or ws2_32.lib?
Yes, but...

In case of http you need to send an http request to the server and parse the response according to the http protocol.

There are libraries that helps with the correct handling of the protocol:

https://pocoproject.org/
https://curl.haxx.se/libcurl/
I would do it like this:
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 <windows.h>
#include <stdio.h>

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

const char szFileName[] = "Download.txt";
const char szUrl[] = "www.yoururl.com";

bool show_file()
{
  // TODO
  // read the file and print the content.
}

int main()
{
  // https://msdn.microsoft.com/en-us/library/ms775123(v=vs.85).aspx

  HRESULT result = URLDownloadToFile(0, szUrl, szFileName, 0, 0);
  switch (result)
  {
    case INET_E_DOWNLOAD_FAILURE:
      printf("\a\nThe specified resource or callback interface was invalid.");
      break;
    case E_OUTOFMEMORY:
      printf("\a\nThe buffer length is invalid, or there is insufficient memory to complete the operation.");
      break;
    case S_OK:
      printf("Success downloading...");
      show_file();
      break;
  }
  system("pause");
  return 0;
}
Topic archived. No new replies allowed.