Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Downloading a file from a URL in C (http://www.go4expert.com/forums/downloading-file-url-c-t21834/)

hraja 19Apr2010 03:58

Downloading a file from a URL in C
 
Hello,

I was wondering if someone could please give me some help in opening a URL in C source code (on Ubuntu).

I have to download an XML file from a URL in one of my programs and save it on disk for parsing.

Any help would be much appreciated. Thank you very much.

SaswatPadhi 22Apr2010 21:48

Re: Downloading a file from a URL in C
 
On Ubuntu, you can do it using a system call to WGET.
Some thing like :

Code: CPP

#include <iosteam>
#include <cstdlib>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string line;

    if(system("wget www.google.com")==0)
    {
        cout << "ok\n";
        ifstream file ("index.html");
        while (!myfile.eof())
        {
            getline(file,line);
            cout<<line<<"\n";
        }
        file.close();
    }
    else
        cout << "NOT ok\n";
    return 0;
}


davidk 27Apr2010 13:36

Re: Downloading a file from a URL in C
 
In case you don't have wget installed or you need some more control over downloading the file, you should use system sockets and look into HTTP RFC.
This is more complex than just executing external programs, but this knowledge will help you in the future anyway.


All times are GMT +5.5. The time now is 16:59.