Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7882937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:24:12+00:00 2026-06-03T04:24:12+00:00

I got a function off of a friend that he found on the net.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:24:13+00:00Added an answer on June 3, 2026 at 4:24 am

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a Function that returns a Collection<string> , and that calls itself recursively
I've got a function I wrote quite some time ago that works fine, but
So, I've got this code: $(document).ready(function(){ $('form').attr('autocomplete', 'off'); }); It works great for all
I've got a function that creates two tables. I've made every variable between the
I've got a function that runs a user generated Regex. However, if the user
I've got a function creating some XmlDocument: public string CreateOutputXmlString(ICollection<Field> fields) { XmlWriterSettings settings
I've got a function here that is meant to look through a list of
I've got a function that does (in short): my $file = IO::File->new(| some_command >>
I've got a popup that I don't want to close unless you click off
I got a problem with the shell_exec php function, here is a example code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.