Question

I want to get a HTML and use like a file in C. Actually I can do that, but I have to save the file first on the disk and then use fopen("/file.html", "r");. What I would like to do is to extract the html directly from the URL and work with it.

Hypothetically, fopen("http://www.google.com", "r");

I saw something about libcurl but I don't know if it will help me.

Was it helpful?

Solution

You can do something as simple as:

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
    res = curl_easy_perform(curl);

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}

Source: Using cURL in C

You can get the complete function documentations here: Using The libcurl C Interface

This PDF link will help you in using libcurl in VS

OTHER TIPS

Yes, I think cURL would be a good choice.

C libcurl get output into a string

libcurl should get you what you need. See the code sample here: http://curl.haxx.se/libcurl/c/fopen.html

This will allow you to "read remote streams instead of (only) local files".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top