?
Solved

Retrieving Files from the Web in C++

Posted on 2005-03-09
7
Medium Priority
?
266 Views
Last Modified: 2010-04-01
I actually have two questions, if you don't mind. :)

1. How do you download a file from the web in c++ code. All I can do now is open up a file with the default browser, but it still asked the user where to save the file, and if it exists, whether they want to overwrite files. I just want the user to be able to click a button and the file will download (to where the application is) and automatically rewrite/replace any previous files without prompting the user.

2. I want to be able to remove a directory in C++, again, without prompts. I tried

Console::WriteLine("RD /S /Q C:\Progra~1\World~1\Interface");

Perhaps i forgot to use "\\" instead of "\". Regardless, I would enjoy not having to use a console. :)


Thanks for all your help, time and time again. :)

-Nathanmx
0
Comment
Question by:nathanmx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 14

Assisted Solution

by:trigger-happy
trigger-happy earned 100 total points
ID: 13503751
I actually made my own RSS client for BBC news and created my own file downloader in doing so, here's my code, feel free to use it. I didn't really have enough time to modify it for your case but i'm sure you can work it out yourself. The Execute functions is  so that i can open the browser and please don't mind the way i named the files and functions ;-)

------------------grabber.h---------------------
#ifndef GRABBER_H
#define GRABBER_H

void Grab();
void Execute(char URL[800]);

#endif
-----------------------------------------------------
}
-----------------grabber.cpp--------------------
#include "grabber.h"

#include <iostream>

#include <fstream>

#include <winsock2.h>



using namespace std;



void Grab(){

      WSADATA wsadata;

      if(WSAStartup(MAKEWORD(2, 2), &wsadata) != NO_ERROR){

            cout << "Error initializing winsock\n" << endl;

            return;

      }



      SOCKET client[7];

      sockaddr_in service;



      for(int a = 0; a < 7; a++){

            client[a] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

            if(client[a] == INVALID_SOCKET){

                  cout << "Failed to create a socket\n" << endl;

                  WSACleanup();

                  return;

            }

      }



      service.sin_family = AF_INET;

      service.sin_port = htons(80);

      service.sin_addr.S_un.S_addr = inet_addr("212.58.240.132");



      char Getter[500];

      char Data[8000];

      ofstream fout;

      int y;



      for(int i = 0; i<7; i++){

            y = 0;

            cout << "Conecting..." << endl;

            if(connect(client[i], (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR){

                  cout << "Error connecting to server\n" << endl;

            }else{

                  strcpy(Getter, "GET ");

                  strcat(Getter, "/rss/newsonline_world_edition/");

                  cout << "Connected" << endl;

                  switch(i){

                        case 0:

                              fout.open("1.xml", ios::out | ios::trunc);

                              strcat(Getter, "front_page/rss091.xml\r\n\r\n");

                              send(client[i], Getter, strlen(Getter), 0);

                              cout << "Downloading  frontpage" << endl;

                              while(y = recv(client[i], Data, 512, 0)){

                                    fout.write(Data, y);

                              }

                              cout << "Done\n" << "Disconnecting...\n" << endl;                              

                              break;

                        case 1:

                              fout.open("2.xml", ios::out | ios::trunc);

                              strcat(Getter, "asia-pacific/rss091.xml\r\n\r\n");

                              send(client[i], Getter, strlen(Getter), 0);

                              cout << "Downloading  asia-pacific" << endl;

                              while(y = recv(client[i], Data, 512, 0)){

                                    fout.write(Data, y);

                              }

                              cout << "Done\n" << "Disconnecting...\n" << endl;                              

                              break;

                        case 2:

                              fout.open("3.xml", ios::out | ios::trunc);

                              strcat(Getter, "business/rss091.xml\r\n\r\n");

                              send(client[i], Getter, strlen(Getter), 0);

                              cout << "Downloading  business" << endl;

                              while(y = recv(client[i], Data, 512, 0)){

                                    fout.write(Data, y);

                              }

                              cout << "Done\n" << "Disconnecting...\n" << endl;                        

                              break;

                        case 3:

                              fout.open("4.xml", ios::out | ios::trunc);

                              strcat(Getter, "health/rss091.xml\r\n\r\n");

                              send(client[i], Getter, strlen(Getter), 0);

                              cout << "Downloading  health" << endl;

                              while(y = recv(client[i], Data, 512, 0)){

                                    fout.write(Data, y);

                              }

                              cout << "Done\n" << "Disconnecting...\n" << endl;                              

                              break;

                        case 4:

                              fout.open("5.xml", ios::out | ios::trunc);

                              strcat(Getter, "science/nature/rss091.xml\r\n\r\n");

                              send(client[i], Getter, strlen(Getter), 0);

                              cout << "Downloading  science" << endl;

                              while(y = recv(client[i], Data, 512, 0)){

                                    fout.write(Data, y);

                              }

                              cout << "Done\n" << "Disconnecting...\n" << endl;                              

                              break;

                        case 5:

                              fout.open("6.xml", ios::out | ios::trunc);

                              strcat(Getter, "technology/rss091.xml\r\n\r\n");

                              send(client[i], Getter, strlen(Getter), 0);

                              cout << "Downloading  technology" << endl;

                              while(y = recv(client[i], Data, 512, 0)){

                                    fout.write(Data, y);

                              }

                              cout << "Done\n" << "Disconnecting...\n" << endl;                              

                              break;

                        case 6:

                              fout.open("7.xml", ios::out | ios::trunc);

                              strcat(Getter, "entertainment/rss091.xml\r\n\r\n");

                              send(client[i], Getter, strlen(Getter), 0);

                              cout << "Downloading  entertainment" << endl;

                              while(y = recv(client[i], Data, 512, 0)){

                                    fout.write(Data, y);

                              }

                              cout << "Done\n" << "Disconnecting...\n" << endl;

                              break;

                  }

                  fout.close();

            }

      }



      for(int j = 0; j < 7; j++){

            closesocket(client[j]);

      }

      WSACleanup();

      system("cls");

}



void Execute(char URL[800]){

      ShellExecute(NULL, "open", URL, NULL, NULL, SW_NORMAL);

-----------------------------------------------------

--trigger-happy
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 200 total points
ID: 13503851
If you are working in windows environment, you need this to delete an empty directory:

RemoveDirectory("C:\\Progra~1\\World~1\\Interface");  

To delete an entire branch, it is a little harder:

SHFILEOPSTRUCT fileOp;
ZeroMemory ( &fileOp, sizeof (fileOp));
fileOp.pFrom ="C:\\Progra~1\\World~1\\Interface";  
fileOp.wFunc = FO_DELETE;
fileOp.fFlags = FOF_SILENT | FOF_NOCONFIRMATION;
SHFileOperation(&fileOp);

And, yes, you have to use double backslashes...
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13503901
Continuing with Windows solutions, you can download in one step with this function:
http://msdn.microsoft.com/library/default.asp?url=/workshop/networking/moniker/reference/functions/urldownloadtofile.asp

Notice you will need to #include "urlmon.h" file into your source code, and link urlmon.lib library to your project.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Assisted Solution

by:novitiate
novitiate earned 200 total points
ID: 13503997
0
 
LVL 1

Author Comment

by:nathanmx
ID: 13514750
This is slightly beyond my skill level, I think. :( I can't get any of it to work. Could you give me a sample pogram that downloads a file, perhaps? I would greatly appreciate it.
0
 
LVL 1

Author Comment

by:nathanmx
ID: 13902200
Sorry, I was hoping someone would still answer. Anyways I've found a temporary way to avoid my problem, and I care not about my points so I'll split them up.

Thanks for your time. ;)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question