1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Downloading a file from a URL in C

Discussion in 'C++' started by hraja, Apr 18, 2010.

  1. hraja

    hraja New Member

    Joined:
    Apr 6, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    On Ubuntu, you can do it using a system call to WGET.
    Some thing like :

    Code:
    #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;
    }
    
     
  3. davidk

    davidk New Member

    Joined:
    Mar 25, 2010
    Messages:
    16
    Likes Received:
    1
    Trophy Points:
    0
    Home Page:
    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.
     

Share This Page