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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题