0
我的代碼如下所示,輸入用戶輸入的URL並打印頁面的HTML代碼內容。代碼的()循環的第一個是爲了去掉HTML中的< ...>之間的代碼,但我似乎無法讓它工作,它不會打印任何東西。C Linux HTML條不工作
任何人都可以看到,如果我的代碼有問題,它工作正常,但不會刪除或打印任何東西后上述for()循環。
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string.h>
/*
*
*/
size_t write_data(void *ptr, size_t size, size_t nmeb, void *stream) {
return fwrite(ptr, size, nmeb, stream);
}
/* function prototypes to define later */
char *do_web_request(char *url);
size_t static write_callback_func(void *buffer,
size_t size,
size_t nmemb,
void *userp);
int main(int argc, char** argv) {
char str[100];
int len;
printf("Enter the website URL:\n");
scanf("%s", str);
char *content = NULL;
content = do_web_request(str);
len = strlen(str);
int i;
int idx = 0;
int opened = 0;
printf("Original content::");
printf("%s", content);
printf("\nReplaced String ");
for (i = 0; i < len; i++) {
if (content[i] == '<') {
opened = 1;
} else if (content[i] == '>') {
opened = 0;
} else if (!opened) {
content[idx++] = content[i];
}
}
content[idx] = '\0';
printf("%s\n", content);
return (EXIT_SUCCESS);
}
/* the function to return the content for a url */
char *do_web_request(char *str) {
/* keeps the handle to the curl object */
CURL *curl_handle = NULL;
/* to keep the response */
char *response = NULL;
/* initializing curl and setting the url */
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, str);
curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1);
/* follow locations specified by the response header */
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
/* setting a callback function to return the data */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback_func);
/* passing the pointer to the response as the callback parameter */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &response);
/* perform the request */
curl_easy_perform(curl_handle);
/* cleaning all curl stuff */
curl_easy_cleanup(curl_handle);
return response;
}
/* the function to invoke as the data recieved */
size_t static write_callback_func(void *buffer,
size_t size,
size_t nmemb,
void *userp) {
char **response_ptr = (char**) userp;
/* assuming the response is a string */
*response_ptr = strndup(buffer, (size_t) (size * nmemb));
}
你是對的。愚蠢的錯誤,我的一部分。謝謝。 – user667430 2012-02-26 13:11:39