【问题标题】:Read .html file into string in c++在 C++ 中将 .html 文件读入字符串
【发布时间】:2012-06-09 18:07:10
【问题描述】:

我正在寻找一些技巧,如何通过 HTTP 从远程服务器读取字符串并将其保存到字符串中。目前,我的目标是阅读第一个字符。

@编辑: 我已经做了什么:

我认为 curl 可能是实现这一目标的正确工具。

编译:

1>------ Rebuild All started: Project: cURL, Configuration: Debug Win32 ------
1>  stdafx.cpp
1>  cURL.cpp
1>  cURL.vcxproj -> C:\Users\Lukasz\Documents\Visual Studio 2010\Projects\cURL\Debug\cURL.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

但调试后:

The procedure entry point sasl_errdetail could not be located in the dynamic link library libsasl.dll

我的主文件.cpp:

// cURL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


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


std::string buffer;

size_t curl_write( void *ptr, size_t size, size_t nmemb, void *stream)
{
buffer.append((char*)ptr, size*nmemb);
return size*nmemb;
}

int main(int argc, char **argv)
{
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com"); 
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
fwrite( buffer.c_str(), buffer.length(), sizeof(char), stdout);
return 0;
}

【问题讨论】:

  • 本地文件系统上的文件,还是通过 HTTP 提供的文件?
  • 好的。为什么不想使用 CURL?
  • 只有一个字符?那不是&lt;吗?或者在任何情况下都不会映射到false
  • 因为首先我用它工作了 5 个小时,但它无法工作......最后当它突然被编译并且我开始调试它时显示 libsasl.dll 和过程 sasl_errdetail 的错误或那样的东西;/
  • 发布您的尝试和遇到的错误。

标签: c++ visual-studio-2010 file


【解决方案1】:

我遇到了同样的问题!

从 dlldll.com 下载 libsasl.dll 并得到同样的错误。我以为他们有旧版本的 dll,所以我下载了新版本,它就像一个魅力。

我从来没有从http://theetrain.ca/tech/files/libsasl.dll得到版本

【讨论】:

    【解决方案2】:
    1. 打开文件。
    2. 使用 fgetc 或 istream::read 函数。
    3. 将字符转换为正确的布尔值。
    4. 关闭文件。

    HTML 文件可以像文本文件一样读取。

    解析 HTML 文件是另一个问题。

    【讨论】:

      【解决方案3】:

      只需在服务器/客户端设置中接收 html,您就需要连接到套接字并处理来自套接字的消息。

      这是一个在 Windows 上介绍 Sockets 的好例子:

      Programming Windows TCP Sockets in C++ for the Beginner

      如果您想完全避免学习套接字并真正很好地控制正在发生的事情,您可以只使用各种 URL 下载器:

      Download a URL in C

      但如果您需要速度胜过其他任何事情,使用 URLOpenStream 可能会给您最快的结果。

      所以当你有了一个接收 html 文本的 socket 之后,你就可以解析 html 并将你找到的内容转换成合适的变量。

      【讨论】:

        猜你喜欢
        • 2011-03-18
        • 2016-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        • 1970-01-01
        相关资源
        最近更新 更多