可以使用 libcurl 将远程文件作为 istream 读取吗?

发布于 2024-09-05 11:22:24 字数 239 浏览 4 评论 0原文

我想使用 libcurl 库打开远程日期文件并使用 istream 对其进行迭代。我已经浏览了这个 线​​程 中的好示例,但它写入了远程文件到本地文件。相反,我希望将远程读取推送到 istream 以进行后续的编程操作。这可能吗?我将非常感谢帮助。

最好的, 亚伦

I'd like to use the libcurl library to open a remote date file and iterate through it with an istream. I've looked through the nice example in this thread but it writes the remote file to a local file. Instead I'd like to have the remote reads be pushed to an istream for subsequent programmatic manipulation. Is this possible? I would greatly appreciate help.

Best,
Aaron

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青瓷清茶倾城歌 2024-09-12 11:22:24

Boost 的 IO Stream 可能是更好的解决方案比STL自己的流。至少创建 boost 流要简单得多。来自 boost 自己的文档:

#include <curl/curl.h>
#include <boost/iostreams/stream.hpp>

class CURLDevice
{
    private:
        CURL* handle;
    public:
        typedef char                            char_type;
        typedef boost::iostreams::source_tag    category;

        CURLDevice()
        {
            handle = curl_easy_init();
        }

        CURLDevice(const std::string &url)
        {
            handle = curl_easy_init();
            open( url );
        }

        ~CURLDevice()
        {
            curl_easy_cleanup(handle);
        }

        void open(const std::string &url)
        {
            curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
            curl_easy_setopt(handle, CURLOPT_CONNECT_ONLY, 1);
            curl_easy_perform(handle);
        }

        std::streamsize read(char* s, std::streamsize n)
        {
            size_t read;
            CURLcode ret = curl_easy_recv(handle, s, n, &read);
            if ( ret == CURLE_OK || ret == CURLE_AGAIN )
                return read;
            else
                return -1;
        }
};

typedef boost::iostreams::stream<CURLDevice> CURLStream;

int main(int argc, char **argv)
{
    curl_global_init(CURL_GLOBAL_ALL);

    {
        CURLStream stream("http://google.com");

        char buffer[256];
        int sz;

        do
        {
            sz = 256;
            stream.read( buffer, sz );
            sz = stream.gcount();
            std::cout.write( buffer, sz );
        }
        while( sz > 0 );
    }

    curl_global_cleanup();

    return 0;
}

注意:当我运行上面的代码时,我在 CURL 中遇到了段错误,这似乎是因为我不知道如何使用curl 本身。

Boost's IO Stream might be a better solution than STL's own stream. At least it is much simpler to create a boost stream. From boost's own docs:

#include <curl/curl.h>
#include <boost/iostreams/stream.hpp>

class CURLDevice
{
    private:
        CURL* handle;
    public:
        typedef char                            char_type;
        typedef boost::iostreams::source_tag    category;

        CURLDevice()
        {
            handle = curl_easy_init();
        }

        CURLDevice(const std::string &url)
        {
            handle = curl_easy_init();
            open( url );
        }

        ~CURLDevice()
        {
            curl_easy_cleanup(handle);
        }

        void open(const std::string &url)
        {
            curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
            curl_easy_setopt(handle, CURLOPT_CONNECT_ONLY, 1);
            curl_easy_perform(handle);
        }

        std::streamsize read(char* s, std::streamsize n)
        {
            size_t read;
            CURLcode ret = curl_easy_recv(handle, s, n, &read);
            if ( ret == CURLE_OK || ret == CURLE_AGAIN )
                return read;
            else
                return -1;
        }
};

typedef boost::iostreams::stream<CURLDevice> CURLStream;

int main(int argc, char **argv)
{
    curl_global_init(CURL_GLOBAL_ALL);

    {
        CURLStream stream("http://google.com");

        char buffer[256];
        int sz;

        do
        {
            sz = 256;
            stream.read( buffer, sz );
            sz = stream.gcount();
            std::cout.write( buffer, sz );
        }
        while( sz > 0 );
    }

    curl_global_cleanup();

    return 0;
}

Note: when I run the code above I get a segfault in CURL, this appears to be because I don't know exactly how to use curl itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文