1

I would like to download an html file using c++. I have some code that works with Visual Studio but I need it to run in unix and be able to be compiled with gcc. I found a lot of questions similar to this with good answers but nothing that works in unix. Here is my code that works perfectly in visual studio...

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

using namespace std;

int main()
{
    char webAddress[256] = "https://www.ibm.com/us-en/?ar=1";
    char szFileName[80] = "ibm.html";



    HRESULT hr = URLDownloadToFile(NULL, webAddress, szFileName,0, NULL);

    if (hr == S_OK)
    {
        ifstream fin(szFileName);
        char szBuff[2048];

    }
    else
    {
        cout << "Operation failed with error code: " << hr << "\n";
    }

    return 0;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    `URLDownloadToFile` function is a Windows only function and will not work on Linux as there is no `URLDownloadToFile` on Linux. Furthermore, this function has nothing to do with Visual Studio either. – Ron May 07 '19 at 15:11
  • 1
    Maybe look for [libcurl](https://curl.haxx.se/libcurl) – Bodo May 07 '19 at 15:19
  • 1
    There are plenty of http client libraries you can use. `libcurl` is one, `Qt` contains another and there are many more. – Jesper Juhl May 07 '19 at 15:21
  • This has not much to do with HTML and everything to do with HTTP and networking – Lightness Races in Orbit May 07 '19 at 15:27

2 Answers2

3

You might like to use libCURL, which is almost exactly what you describe.

There are sample applications here, and in particular this demonstrates how simple usage can be.

ref.

Mayur
  • 2,583
  • 16
  • 28
1

I wish there was some more context but you might be able to look at other programs like wget. wget is a common tool used for exactly this and it's old enough that it should have a simple makefile with few dependencies. Most Linux systems come with it preinstalled. CPP is almost backwards compatible with C, I can't say for certain but I work with C and often use CPP compilers when GCC is acting up.

Source for wget https://www.gnu.org/software/wget/

You should be able to build it from that or if you are determined to make your own use the source as a reference.

I hope this helps.