Libcurl output to variable instead of textfile?

You could use a string stream to save the data in memory. You would create the string stream in main and pass a pointer to it to curl, like you currently pass a pointer to a FILE std::ostringstream stream; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream); // call curl like before // ... // then get result as a std::string: std::string output = stream.get() The according write_data function would look like this: size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata) { std::ostringstream *stream = (std::ostringstream*)userdata; size_t count = size * nmemb; stream->write(ptr, count); return count; } And about the function calling, it works basically the same way as in other languages. You have to give parameters to the function if you call it.

The difference in this case is, that in curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data) the write_data function isn't called write_data is instead given as a parameter to curl_easy_setopt which informs curl that is should use this function when it needs to write data Later then, once curl receives data and wants to write that data, it will call write_data with all the appropriate parameters.

You could use a string stream to save the data in memory. You would create the string stream in main and pass a pointer to it to curl, like you currently pass a pointer to a FILE. Std::ostringstream stream; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream); // call curl like before // ... // then get result as a std::string: std::string output = stream.get(); The according write_data function would look like this: size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata) { std::ostringstream *stream = (std::ostringstream*)userdata; size_t count = size * nmemb; stream->write(ptr, count); return count; } And about the function calling, it works basically the same way as in other languages.

You have to give parameters to the function if you call it. The difference in this case is, that in curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data) the write_data function isn't called. Write_data is instead given as a parameter to curl_easy_setopt which informs curl that is should use this function when it needs to write data.

Later then, once curl receives data and wants to write that data, it will call write_data with all the appropriate parameters.

Thanks for explaining, can't get it to work yet, though. Invalid use of incomplete type 'struct std::basic_ostringstream, std::allocator >' which happens at the line stream->write(ptr, count); in your write_data function. – natli Nov 5 at 14:53 You need #include to get the definition of std::ostringstream.

– sth Nov 5 at 15:56 Thanks, beginner mistake ;) – natli Nov 5 at 16:35.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions