Originally Posted by
std10093
You are checking the wrong value i would say
Code:
for(array_ctr = 0; ctr < data[0].x; array_ctr++)
printf("%s\n", data[array_ctr].string1);
This should be array_ctr maybe... Try this out and let us know
Thanks. Works now. If anyone can suggest better code for achieving the same thing, I'd really like to hear it. Seems like my program has a lot of code, for a rather simple result.
Now I can apply this method to my main program below, but that is for another thread.
Code:
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define URL_LENGTH 100
struct MemoryStruct {
char *memory;
size_t size;
};
struct LinkStruct {
char *link;
int x;
};
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp);
struct LinkStruct * GetLinks(struct MemoryStruct);
int main(void)
{
curl_global_init(CURL_GLOBAL_ALL);
CURL *myhandle;
CURLcode result;
struct MemoryStruct datastruct;
datastruct.memory = malloc(1); // will be grown as needed by realloc
datastruct.size = 0; // no data at this point
if((myhandle = curl_easy_init()) == NULL)
{
perror("curl_easy-init error");
exit(EXIT_FAILURE);
}
if((result = curl_easy_setopt(myhandle, CURLOPT_URL, "http://cboard.cprogramming.com/c-programming/")) != CURLE_OK)
{
perror("CURLOP_URL error");
exit(EXIT_FAILURE);
}
if((result = curl_easy_setopt(myhandle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback)) != CURLE_OK)
{
perror("CURLOPT_WRITEFUNCTION error");
exit(EXIT_FAILURE);
}
if((result = curl_easy_setopt(myhandle, CURLOPT_WRITEDATA, &datastruct)) != CURLE_OK)
{
perror("CURLOPT_WRITEDATA error");
exit(EXIT_FAILURE);
}
if((result = curl_easy_perform(myhandle)) != 0)
{
perror("curl_easy_perform error");
exit(EXIT_FAILURE);
}
curl_easy_cleanup(myhandle);
GetLinks(datastruct);
return 0;
}
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory == NULL) /* out of memory! */
{
printf("not enough memory (realloc returned NULL)\n");
exit(EXIT_FAILURE);
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
struct LinkStruct * GetLinks(struct MemoryStruct datastruct)
{
char *new_string;
struct LinkStruct links[50];
char *search_string = "http://cboard.cprogramming.com/c-programming/";
int ch; // test character
int ctr = 0, url_count = 0;
int location; // location of matching strings
int dblqut; // location of "
dblqut = strlen(search_string) + 1;
while((ch = datastruct.memory[ctr]) != '\0')
{
location = strstr(datastruct.memory, search_string) - datastruct.memory;
if((ch = datastruct.memory[location + dblqut]) == '"')
{
}
}
}