-2

I need to batch download a file from a link and this link is in a string, how do I do it? I downloaded curl but I don't know how to use it.
string goes this way:
www.example.com/item1.jpeg
www.example.com/item2.jpeg
and so on.
I don't need to change the output names, they can stay as they are.

I'm using this:

CURL curl; 
CURLcode res; 
curl = curl_easy_init(); 
if(curl) { 
    curl_easy_setopt(curl, CURLOPT_URL, c_str(link)); 
    res = curl_easy_perform(curl); 
    /* always cleanup */ 
    curl_easy_cleanup(curl);
}

But I'm getting the error:

[Error] 'c_str' was not declared in this scope

My whole script is:

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>

using namespace std;

int main ()
{
  char buffer[21];
  int start;
  int end;
  int counter;
  string site;
  site = "http://www.example.com/";
  string extension;
  extension= ".jpeg";
  string link;
  cout << "Start: ";
  cin >> start;
  cout << "End: ";
  cin >> end;
  for (counter=start; counter<=end; counter++)
  {
       std::string link = site+itoa(counter, buffer, 10)+extension;
      cout << link;
      cout << "\n";
          ////////////////////////////////////////////////////////////////////////////////    /////////////////////////////////////////////////////
  CURL curl; 
  CURLcode res; 
  curl = curl_easy_init(); 
  if(curl) { 
    curl_easy_setopt(curl, CURLOPT_URL, link.c_str()); 
    res = curl_easy_perform(curl); 
    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
      ////////////////////////////////////////////////////////////////////////////////    /////////////////////////////////////////////////////
  }
  return 0;
}

The error is still there.

Oliver
  • 926
  • 2
  • 12
  • 31
  • 1
    Have you reviewed the [curl documentation](http://curl.haxx.se/libcurl/c/10-at-a-time.html)? What tutorials did you look at? – PaulProgrammer Oct 27 '15 at 20:00
  • Hi, I read this (http://curl.haxx.se/libcurl/c/libcurl-tutorial.html) and this example (https://siddhantahuja.wordpress.com/2009/04/12/how-to-download-a-file-from-a-url-and-save-onto-local-directory-in-c-using-libcurl/), other than this comment on stackoverflow (http://stackoverflow.com/a/1636415/5495385)... I just downloaded and installed libcurl but I cant use it indeed.. – Oliver Oct 27 '15 at 20:08
  • Why can't you use it? Are you having issues in compilation, or have you written some code that produces a binary that doesn't behave as you expect? You need to provide us a starting place to give help -- what you've effectively done with your question is ask the community to write software for you. – PaulProgrammer Oct 27 '15 at 20:11

1 Answers1

1

The error regarding c_str has nothing to do with curl. Instead, it indicates you have not used C++ strings correctly. Reviewing the documents one can see that c_str is a method of the string object.

http://www.cplusplus.com/reference/string/string/c_str/

So, in all likelihood, you need to have something of the following form:

#include <string>
#include <curl/curl.h>

int main () {
  std::string link ("http://www.example.com/foo1.jpg");
  CURL curl; 
  CURLcode res; 
  curl = curl_easy_init(); 
  if(curl) { 
    curl_easy_setopt(curl, CURLOPT_URL, link.c_str()); 
    res = curl_easy_perform(curl); 
    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
}
PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56