473,785 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What am I doing wrong? curl

I want to store result of curl in a variable, which means to store a
webpage in a variable in order to parse it later and get our some useful
information. But unfortunately I'm not too experienced in C and I'm
making some stupid mistake. Could someone help me solve it?
#include <string.h>
#include <curl/curl.h>
size_t write_data(cons t char *buffer, size_t size, size_t nmemb, char
*userp)
{
char *string = userp;
size_t len;
len = size * nmemb;
strncat(string, buffer,len); // I get segfault here. Why????
return len;
}

int main(int argc, char **argv)
{
char *tablica="";
CURL *curl;
CURLcode success;
curl = curl_easy_init( );
if(curl) {
curl_easy_setop t(curl, CURLOPT_URL, "http://debian.org/");//just a
sample url.
curl_easy_setop t(curl, CURLOPT_WRITEFU NCTION, write_data);
curl_easy_setop t(curl, CURLOPT_WRITEDA TA, tablica);
success = curl_easy_perfo rm(curl);
curl_easy_clean up(curl);
}
return 0;
}

Thanks in advance for your help.
Nov 15 '05 #1
6 10324
On Mon, 05 Sep 2005 21:18:29 +0200, Shutdownrunner
<"shutdownrunne r[NOSPAM]"@o2.pl> wrote:
I want to store result of curl in a variable, which means to store a
webpage in a variable in order to parse it later and get our some useful
information. But unfortunately I'm not too experienced in C and I'm
making some stupid mistake. Could someone help me solve it?
#include <string.h>
#include <curl/curl.h>
And we know what is in this header because ...


size_t write_data(cons t char *buffer, size_t size, size_t nmemb, char
*userp)
{
char *string = userp;
size_t len;
len = size * nmemb;
strncat(string, buffer,len); // I get segfault here. Why????
write_data is called only through curl_easy_setop t. We have no idea
no idea how that is accomplished. What do buffer and userp point to?
return len;
}

int main(int argc, char **argv)
{
char *tablica="";
CURL *curl;
CURLcode success;
curl = curl_easy_init( );
if(curl) {
curl_easy_setop t(curl, CURLOPT_URL, "http://debian.org/");//just a
sample url.
curl_easy_setop t is a function taking three arguments. The third one
has a type consistent with pointer to char. It could be pointer to
void or const qualified.
curl_easy_setop t(curl, CURLOPT_WRITEFU NCTION, write_data);
Here you call curl_easy_setop t and the third argument is a function
pointer. This is completely inconsistent unless curl_easy_setop t is a
variadic function. Is it?
curl_easy_setop t(curl, CURLOPT_WRITEDA TA, tablica);
success = curl_easy_perfo rm(curl);
curl_easy_clean up(curl);
}
return 0;
}

Thanks in advance for your help.

<<Remove the del for email>>
Nov 15 '05 #2
Barry Schwarz napisa³(a):
curl_easy_setop t(curl, CURLOPT_WRITEFU NCTION, write_data);

Here you call curl_easy_setop t and the third argument is a function
pointer. This is completely inconsistent unless curl_easy_setop t is a
variadic function. Is it?

I don't know if I understand you correctly, because I haven't heard
about variadic functions before. There's a paragraph in libcurl which says:
"Let's assume for a while that you want to receive data as the URL
identifies a remote resource you want to get here. Since you write a
sort of application that needs this transfer, I assume that you would
like to get the data passed to you directly instead of simply getting it
passed to stdout. So, you write your own function that matches this
prototype:

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);

You tell libcurl to pass all data to this function by issuing a function
similar to this:

curl_easy_setop t(easyhandle, CURLOPT_WRITEFU NCTION, write_data); "

Userp can be FILE, but I wanted it to be a variable.
curl_easy_setop t(curl, CURLOPT_WRITEDA TA, tablica);


I'm passing tablica to write_data, because I want all that is downloaded
by curl to be stored in this variable. I still have a lot to read about
local and global variables, so it might be not that necessary as I
think. Please tell me if there's any better solution to do what I want
to do.

I'm not getting any errors anymore. Unfortunately I only found a
temporary solution.
char tablica[100000];
tablica[0] = '\0';
I'm so used to PHP and not being forced to worry about memory
management:( Is it possible to change variables size at runtime? Because
I never know how big a webpage will be. E.g. I declare an empty
variable, read the buffer, see that it is currently e.g. 1000bytes long,
so I make my variable 1000bytes long, then I read buffer again and I see
than now it's 2000bytes long, so I add another 2000 bytes to my variable
(to make it 3000 bytes long), and so on and so forth. How do I do that?

Best regards,
Jacek
Nov 15 '05 #3
I'm not getting any errors anymore. Unfortunately I only found a
temporary solution.
char tablica[100000];
tablica[0] = '\0';
I'm so used to PHP and not being forced to worry about memory
management:( Is it possible to change variables size at runtime? Because
I never know how big a webpage will be. E.g. I declare an empty
variable, read the buffer, see that it is currently e.g. 1000bytes long,
so I make my variable 1000bytes long, then I read buffer again and I see
than now it's 2000bytes long, so I add another 2000 bytes to my variable
(to make it 3000 bytes long), and so on and so forth. How do I do that?


So this is not too hard to do if you can tell how big the buffer needs
to be before you copy it to tablica. You can manage memory on the fly
by using malloc calls like so...

char *tablica;
....
tablica = (char *) malloc(buffersi ze * sizeof(char));
....
/* To make the buffer bigger: */
/* Free old memory */
free(tablica);
/* Allocate new memory */
tablica = (char *) malloc(newbuffe rsize * sizeof(char));

Ofcourse this loses all the stored values in tablica. To preserve this
data you would need to create a new buffer pointed to by a temp
varialbe and copy the memory over, then free the old buffer and have
tablica point to the new buffer.

Zach

Nov 15 '05 #4
On 5 Sep 2005 15:16:48 -0700, "elzacho" <el*****@gmail. com> wrote:
I'm not getting any errors anymore. Unfortunately I only found a
temporary solution.
char tablica[100000];
tablica[0] = '\0';
I'm so used to PHP and not being forced to worry about memory
management:( Is it possible to change variables size at runtime? Because
I never know how big a webpage will be. E.g. I declare an empty
variable, read the buffer, see that it is currently e.g. 1000bytes long,
so I make my variable 1000bytes long, then I read buffer again and I see
than now it's 2000bytes long, so I add another 2000 bytes to my variable
(to make it 3000 bytes long), and so on and so forth. How do I do that?
So this is not too hard to do if you can tell how big the buffer needs
to be before you copy it to tablica. You can manage memory on the fly
by using malloc calls like so...

char *tablica;
...
tablica = (char *) malloc(buffersi ze * sizeof(char));


Casting the return from malloc only serves to hide real errors.
...
/* To make the buffer bigger: */
/* Free old memory */
free(tablica );
/* Allocate new memory */
tablica = (char *) malloc(newbuffe rsize * sizeof(char));

Ofcourse this loses all the stored values in tablica. To preserve this
data you would need to create a new buffer pointed to by a temp
varialbe and copy the memory over, then free the old buffer and have
tablica point to the new buffer.


Consider the realloc() function.

<<Remove the del for email>>
Nov 15 '05 #5
On Mon, 05 Sep 2005 23:41:28 +0200, Shutdownrunner
<"shutdownrunne r[NOSPAM]"@o2.pl> wrote:
Barry Schwarz napisa³(a):
curl_easy_setop t(curl, CURLOPT_WRITEFU NCTION, write_data);

Here you call curl_easy_setop t and the third argument is a function
pointer. This is completely inconsistent unless curl_easy_setop t is a
variadic function. Is it?

I don't know if I understand you correctly, because I haven't heard
about variadic functions before. There's a paragraph in libcurl which says:
"Let's assume for a while that you want to receive data as the URL
identifies a remote resource you want to get here. Since you write a
sort of application that needs this transfer, I assume that you would
like to get the data passed to you directly instead of simply getting it
passed to stdout. So, you write your own function that matches this
prototype:

snip

You need to discuss this in a newsgroup where libcurl is on-topic.
<<Remove the del for email>>
Nov 15 '05 #6
"elzacho" <el*****@gmail. com> writes:
I'm not getting any errors anymore. Unfortunately I only found a
temporary solution.
char tablica[100000];
tablica[0] = '\0';
I'm so used to PHP and not being forced to worry about memory
management:( Is it possible to change variables size at runtime? Because
I never know how big a webpage will be. E.g. I declare an empty
variable, read the buffer, see that it is currently e.g. 1000bytes long,
so I make my variable 1000bytes long, then I read buffer again and I see
than now it's 2000bytes long, so I add another 2000 bytes to my variable
(to make it 3000 bytes long), and so on and so forth. How do I do that?
So this is not too hard to do if you can tell how big the buffer needs
to be before you copy it to tablica. You can manage memory on the fly
by using malloc calls like so...

char *tablica;
...
tablica = (char *) malloc(buffersi ze * sizeof(char));


Better:

tablica = malloc(buffersi ze);
/* To make the buffer bigger: */
/* Free old memory */
free(tablica);
/* Allocate new memory */
tablica = (char *) malloc(newbuffe rsize * sizeof(char));

Ofcourse this loses all the stored values in tablica. To preserve this
data you would need to create a new buffer pointed to by a temp
varialbe and copy the memory over, then free the old buffer and have
tablica point to the new buffer.


Use realloc() for this.

One thing to watch out for is that realloc(), like malloc() can fail.
If you want to be able to recover from such an error, you need to
assign the result of realloc() to a separate variable; otherwise you
could lose your original data.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2934
by: Haluk Durmus | last post by:
Hello I checked out openssl,mm,apr,apr-util,apache 2,curl,libxml and php from cvs. php couse an ERROR I did the following steps:
0
2952
by: Phil Powell | last post by:
I am having trouble retrieving URLs using curl for PHP whereby the URL requires a cookie to produce proper data. I wrote a wrapper class called Timer that will time the execution/download of a remote URL and it uses curl as its means of obtaining the URL: class Timer extends View { /** * @access private
8
3338
by: mrbog | last post by:
1. In order to make an http (or https) request with PHP, I need to recompile php with cURL. 2. In order to install CURL I have to upgrade my openssl rpm, even though I'm runing a version of linux that's only like 3 months old (fedora c2 stable), because curl needs libcrypto.so.4. 3. In order to install libcrypto.so.4 I have to upgrade my openssl (took me forever to figure that out).
9
8768
by: Dave Martin | last post by:
I've successfully used CURL to maneuver around and through sites but a new site I've been trying to work with has got me stumped. To retrieve the data I'm trying to get a login is required. If you go to any page in the "member" area without logging in you get directed to a https connection and hit login.php (ie: you end up at https://www.domain.com/login.php). The problem, I believe, lies in the fact that the form on that page...
3
6332
by: Chris Fortune | last post by:
# uname -a Linux stargate.mxc-online.net 2.4.20-021stab022.2.777-smp #1 SMP Wed Jul 28 17:12:37 MSD 2004 i686 i686 i386 GNU/Linux I recompiled PHP with mcrypt, openssl, and curl phpinfo(): http://www.canadiandropshipping.com/hello.php3 Does anyone know why this ssl curl test fails? http://www.canadiandropshipping.com...t/diag_curl.php
0
5860
by: nfhm2k | last post by:
I've been trying to find a solution to this for quite some time now... I even took a look at existing scripts... Including this one... http://groups.google.co.uk/group/comp.lang.php/browse_thread/thread/2e052386da903425/b03ec83ac55273a2?lnk=st&q=&rnum=1#b03ec83ac55273a2 Everyone on that post seems to say its to do with the cookie's, yet if infact they had tried this script they would have found that even with the cookies enabled this...
0
1107
by: Joshua Ruppert | last post by:
I'm trying to figure out what I'm doing wrong with my setup Alternative PHP Cache. It's not saving the cache entries. When I look at the APC Info page (apc.php) the only entry I ever see is apc.php. It reports that it was just loaded into cache during that very request, so thus never successfully storing/retrieving it from cache. My setup is: Windows 2003 Standard Edition
0
3370
by: xerc | last post by:
I am trying to create a generic function I can call to download all files from a single remote FTP directory -- using CURL. I want to multi-thread it, but need to get the single thread functionality working first before I tackle that. Anyway, in my function I can list all the files, but the function I have, no matter how I try, will only return one file -- the last file. My for() loop seems pretty straightforward, so not sure why only the...
3
10106
by: rottmanj | last post by:
I am re-writing my rets application in perl, and I have found a few modules that will help me on my way. One of them being WWW::Curl:easy. During my testing, I have tested both system curl and perl curl. At this point I can get the system curl to correctly connect to my server. However, I am having the hardest time trying to figure out why I cant get perl curl to connect to the server. Every time I try to connect, I get a 401 error. I am...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10162
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.