【问题标题】:for loop Download file from string linkfor loop 从字符串链接下载文件
【发布时间】:2015-10-27 19:53:39
【问题描述】:

我需要从一个链接批量下载一个文件,而这个链接是一个字符串,我该怎么做?我下载了 curl 但我不知道如何使用它。
字符串是这样的:
www.example.com/item1.jpeg
www.example.com/item2.jpeg
等等。
我不需要更改输出名称,它们可以保持原样。

我正在使用这个:

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);
}

但我得到了错误:

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

我的整个脚本是:

#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;
}

错误仍然存​​在。

【问题讨论】:

标签: c++ string get int libcurl


【解决方案1】:

c_str 的错误与 curl 无关。相反,它表明您没有正确使用 C++ 字符串。查看文档可以看到c_str是字符串对象的一个​​方法。

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

因此,您很可能需要以下形式的东西:

#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);
  }
}

【讨论】:

  • 您好,感谢您的回复。我更新了原始帖子,因为遗憾的是,它仍然给我同样的错误..
  • 我在编译你最近的尝试时遇到的错误是没有itoa 函数,所以你现在已经解决了c_str 问题...
  • 系统怎么样("wget www.example.com/1.jpeg -q"); ?
猜你喜欢
  • 1970-01-01
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
相关资源
最近更新 更多