Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Retrieving Files from the Web in C++

Posted on 2005-03-09
7
Medium Priority
?
268 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
6 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

564 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