cURL / Mailing Lists / curl-library / Single Mail


Re: help with novice program

From: Frank Mcgeough <>
Date: Tue, 10 Aug 2010 15:09:43 -0400

On Aug 10, 2010, at 2:33 PM, neil sutton wrote:

> Hi, I am trying to write a simple program in C++ that obtains user
> input of a string.
> The curl library is then used to make a connection to a given
> website and then - if possible search for all occurance of the given
> string. My questions are:
> 1. Is it essential to transfer a copy of the online resource before
> searching for the expression? Could my program search the online
> resource remotely?
> 2. This is the simple code i have written so far:
> #include <string>
> #include <iostream>
> #include <curl/curl.h>
> using namespace std;
> // simple program to obtain user input of a string, connect to a
> website to search for occurances of
> // the string
> int main()
> {
> int end(1);
> while (end != 0)
> {
> // Initialisation of curl
> curl_global_init(CURL_GLOBAL_ALL);
> std::string myword;
> cout << "Input a word to search for \n";
> cin >> myword;
> cout << "Searching for occurances of " << myword << endl;
> // insert connection and search code here
> // which website?
> // look for myword
> cout << "Do you wish to search again? ENTER 0 to end or 1 to
> search again\n";
> cin >> end;
> if (end == 0)
> return 0;
> };
> curl_global_cleanup();
> }
> Now suppose I wish to search the URL for all
> occurances of the string myword, how would I actually code this? Im
> not even sure that the code i have written properly invokes curl,
> though it does compile. Any suggestions would be welcome. Once i get
> this program working, i will feel more confident in experimenting
> further.
> Many thanks
> neil

Yes. You'll need to grab the content of the web page and put it into
memory in order to search for string occurrences.

The libcurl examples are good to start with - although they aren't
exactly what you want. ftpget.c for example is a good one. I'd suggest
you get that code and compile it. Step through and see how it
operates. You have to understand the easy handle way of doing things.
The examples should make this clear.

Lets assume that you want to use ftpget.c as a basis for solving your
problem. Since you are writing in C++ and fetching web pages you
probably want to throw the data into a std::string. Instead of using
the my_fwrite callback as the ftpget.c code does you could use
something like this :

size_t writeDataToString(void *buffer, size_t size, size_t nmemb, void
     char *d = (char*)buffer;
     string *b = (std::string *)(userp);

     int result = 0;
     if (b != NULL) {
         b->append(d, size * nmemb);
         result = size * nmemb;
     return result;

The important options to setup the callback so that data goes into
you had declared a std::string curl_buffer in your code. Then on your
easy handle you can do this :

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeDataToString); //
our callback function
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &curl_buffer); // the string
which will contain the html

With these few modifications you can get the web page down and do your
searching. I hope this helps and makes some sense to you. Good luck.

List admin:
Received on 2010-08-10