doulue7522 2012-05-05 00:16
浏览 32
已采纳

使用cURL将数据从网页复制到cpp中的字符串

I got a function off of a friend that he found on the net. Problem is, neither of us understand how it works. It copies the contents of the webpage to the string.

We're using cURL to connect to a php page. The function in question is:

std::string contents;

size_t handle_data(void *ptr, size_t size, size_t nmemb, void *stream)
        {
                    int numbytes = size*nmemb;
                    char lastchar = *((char *) ptr + numbytes - 1);
                    *((char *) ptr + numbytes - 1) = '\0';
                    contents.append((char *)ptr);
                    contents.append(1,lastchar);
                    *((char *) ptr + numbytes - 1) = lastchar;  // Might not be necessary.
                    return size*nmemb;
        }

Its called in this function:

curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,handle_data);

Any explanations into how this works would be great. Cheers.

  • 写回答

2条回答 默认 最新

  • doumiao0498 2012-05-05 00:38
    关注

    I think this is correct:

    std::string contents;
    
    size_t handle_data(void *ptr, size_t size, size_t nmemb, void *stream)
    {
      // The number of bytes to transfer is simply the
      // number of elements (nmemb) times the size of
      // each element (size).
      int numbytes = size*nmemb;
    
      // Remember the last character of the data we'll
      // be appending to 'contents', because we're about
      // to overwrite it with a zero C-string nul terminator.
      char lastchar = *((char *) ptr + numbytes - 1);
    
      // Overwrite the last character of ptr's data block
      // with zero, which is a C-string's nul-terminator.
      *((char *) ptr + numbytes - 1) = '\0';
    
      // Append the C-string which begins at 'ptr' to
      // 'contents'.
      contents.append((char *)ptr);
    
      // Now tack on the last character that we remembered above.
      contents.append(1,lastchar);
    
      // Restore the original data buffer's last character
      // in case the caller expects it to still be the same.
      *((char *) ptr + numbytes - 1) = lastchar;  // Might not be necessary.
    
      // Return the number of bytes  that were appended
      // to 'contents'.
      return size*nmemb;
    }
    

    Having said all that, I'm not sure why the whole guts of handle_data() isn't simply this:

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

    ...which I think would accomplish the exact same thing, only more correctly, since the published version will stop early if the buffer at 'ptr' contains an embedded null character. (Of course that's probably not a use case for this function anyway.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程