【问题标题】:C++: using libcurl and streamsC++:使用 libcurl 和流
【发布时间】:2012-05-13 03:25:20
【问题描述】:

只是想学习 C++。我从刮刀开始。这个想法是我想抓取一堆页面,应用一个正则表达式,并将我的发现写入一个文件。

但是,我目前正试图分配 curl 句柄以写入字符串变量。任何指针(我的意思是提示......)?

#include <stdio.h>
#include <curl/curl.h>
#include <string>
#include <iomanip>
#include <boost/lexical_cast.hpp>

// Typedefs
using std::string;      using boost::lexical_cast;
using std::cout;
using std::endl;
using std::ostringstream;

// CURL directives
static const string BASE_URL = "http://www.XXXXXX.com/action?page=";
static char* USER_AGENT_STRING = "C++ Scraper"; 

// CURL variables
static CURL* curl;
static CURLcode res;

       void setPageOpts(CURL* c, int count, const ostringstream stream) {
            cout << "Got to the set opts method." << endl;

            // Set URL
            string new_url = BASE_URL + lexical_cast<string>(count);
            curl_easy_setopt(curl, CURLOPT_URL, new_url.c_str());
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);
        }

        void setConstOpts(CURL *c) {
            // User agent string
            curl_easy_setopt(curl, CURLOPT_USERAGENT, USER_AGENT_STRING);
            //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
        }

        void loadCurl() {
            int status = curl_global_init(CURL_GLOBAL_ALL);

            if (status != 0) {  
                cout << "An error has occurred with exit status " << status << endl;
                exit(status);
            }   
        }

        int main() {
            loadCurl();
            curl = curl_easy_init();

            if(curl) {
                setConstOpts(curl);

                for (int i = 1; i < 2; ++i) {           
                    const ostringstream stream;

                    setPageOpts(curl, i, &stream);

                    res = curl_easy_perform(curl);  

                    string output = stream.get();
                    cout << "And the data was: " << endl << endl << output << endl;
                }

                curl_easy_cleanup(curl);
            }

            // Everything went as planned
            return 0;
        }

我得到的当前错误:

learn.cpp:15: warning: deprecated conversion from string constant to 'char*'
learn.cpp: In function 'int main()':
learn.cpp:62: error: conversion from 'const std::ostringstream*' to non-scalar type 'std::ostringstream' requested
learn.cpp:67: error: request for member 'get' in 'stream', which is of non-class type 'const std::ostringstream*'

在我看来,我想 CURL 获取内存中流的位置,以便它可以在那里写入。所以我认为我需要使用 & 字符并将其提交。但这似乎不起作用。

【问题讨论】:

    标签: c++ pointers curl reference libcurl


    【解决方案1】:

    第一个警告是针对

    static char* USER_AGENT_STRING = "C++ Scraper"; 
    

    文字是const,所以你的指针应该是指向const char的指针。

    第二个问题是const ostringstream stream。如果流是const,你不能对它做任何改变其状态的事情——比如读或写。

    【讨论】:

      【解决方案2】:

      你的定义:

       void setPageOpts(CURL* c, int count, const ostringstream stream)
      

      很可能会给你带来麻烦。看起来您的意思是在 通过引用 中传递 stream 以便可以更新它:

      void setPageOpts(CURL* c, int count, ostringstream& stream) 
      

      (传递时不要忘记使用c)。

      然后代替这个:

                      const ostringstream stream;
                      setPageOpts(curl, i, &stream);
      

      调用你的函数:

              ostringstream stream;
              setPageOpts(curl, i, stream);
      

      这将允许更新stream

      std::ostringstream 没有::get()。我想你的意思是:

              string output = stream.str();
      

      您可能还会发现this answer 很有用,如果不设置CURLOPT_WRITEFUNCTION 来指定回调函数以使用您的std::ostringstream 指针,当libcurl 尝试write 时,您会遇到访问冲突。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-16
        • 2012-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-16
        • 2014-08-23
        相关资源
        最近更新 更多